老生常談比較排序之堆排序
對于堆排序會涉及一些完全二叉樹知識。對于待排序列{10, 2, 11, 8, 7},把它看成是一顆完全二叉樹,如下圖所示。

堆分為大根堆和小根堆:大根堆表示每個根節(jié)點均大于其子節(jié)點(L(i) >= L(2i) && L(i) >= L(2i + 1)),小根堆表示每個根節(jié)點均小于其子節(jié)點(L(i) <= L(2i) && L(i) <= L(2i + 1))。(在完全二叉樹中第i個節(jié)點的左子節(jié)點為2i,其右字節(jié)點為2i + 1)
本文將以大根堆的構(gòu)建作為示例進行講解。
堆排序的第一步——構(gòu)建初始堆。如何構(gòu)建初始堆呢?根據(jù)定義,關(guān)鍵點在于每個根節(jié)點。觀察上述待排序列的完全二叉樹,不難發(fā)現(xiàn)存在節(jié)點2和節(jié)點10有子節(jié)點,它們是需要關(guān)注的節(jié)點。

如何定位節(jié)點2呢?發(fā)現(xiàn)它是葉子節(jié)點,或者最后一個節(jié)點的父節(jié)點,根據(jù)完全二叉樹的性質(zhì)可知,除根節(jié)點外任意節(jié)點的父節(jié)點的編號為⌊n / 2⌋。已知n = 5,易知節(jié)點2的編號為⌊5 / 2⌋ = ②。比較它與左右子節(jié)點的大小并調(diào)整。

最后剩下根節(jié)點10,已知節(jié)點2的編號為②,② - 1 = ①即得到根節(jié)點10的編號。比較它與左右子節(jié)點的大小并調(diào)整。

調(diào)整完畢后發(fā)現(xiàn)已經(jīng)構(gòu)成了一個“大根堆”,示例中的待排序列較為簡單,再給出一個較為復(fù)雜的待排序列,觀察其構(gòu)建大根堆的過程。對于待排序列{53, 17, 78, 09, 45, 65, 87, 32},將它看成一顆完全二叉樹。

同樣我們來看它所需要關(guān)注的節(jié)點有哪些。

根據(jù)第一個例子,我們很容易能定位節(jié)點09的編號為⌊8 / 2⌋ = ④,節(jié)點78的編號為④ - 1 = ③……,依次類推,發(fā)現(xiàn)了一定的規(guī)律,即需要調(diào)整的節(jié)點位置從⌊n / 2⌋開始依次遞減直到根節(jié)點①結(jié)束(⌊n / 2⌋ ~ 1)?,F(xiàn)在開始調(diào)整。




在第四次調(diào)整結(jié)束后發(fā)現(xiàn)節(jié)點53不滿足大根堆的定義,其右子節(jié)點大于它,此時需要做進一步的向下調(diào)整。

注意向下調(diào)整是每次向上調(diào)整的時候都需要做的判斷是否需要向下調(diào)整,而不是在所有的向上調(diào)整結(jié)束過后再回過頭來向下調(diào)整。這樣大根堆就建立好了,此時待排序列數(shù)組情況已經(jīng)發(fā)生了改變:{87, 45, 78, 32, 17, 65, 53, 09}。接下來是如何進行排序的問題。將大根堆的根節(jié)點與最后一個節(jié)點互換,并調(diào)整二叉樹使其仍然滿足大根堆。

可以看到將根節(jié)點與最后一個節(jié)點呼喚后,待排序列的最大值已經(jīng)放到了數(shù)組的最后一個位置{……, 87},此時完成了第一趟排序,但這第一趟排序還沒有結(jié)束,此時除節(jié)點87外,其余節(jié)點并不滿足大根堆的條件,所以需要對其余節(jié)點進行調(diào)整為大根堆。排序過程不再給出,Java和Python3的代碼實現(xiàn)如下。
Java
package com.algorithm.sort.heap;
import java.util.Arrays;
/**
* 堆排序
* Created by yulinfeng on 6/20/17.
*/
public class Heap {
public static void main(String[] args) {
int[] nums = {53, 17, 78, 09, 45, 65, 87, 32};
nums = heapSort(nums);
System.out.println(Arrays.toString(nums));
}
/**
* 堆排序
* @param nums 待排序數(shù)組序列
* @return 排好序的數(shù)組序列
*/
private static int[] heapSort(int[] nums) {
for (int i = nums.length / 2 - 1; i >= 0; i--) {
heapAdjust(nums, i, nums.length);
}
for (int i = nums.length - 1; i > 0; i--) {
int temp = nums[i];
nums[i] = nums[0];
nums[0] = temp;
heapAdjust(nums, 0, i);
}
return nums;
}
/**
* 調(diào)整堆
*
* @param nums 待排序序列
* @param parent 待調(diào)整根節(jié)點
* @param length 數(shù)組序列長度
*/
private static void heapAdjust(int[] nums, int parent, int length) {
int temp = nums[parent];
int childIndex = 2 * parent + 1; //完全二叉樹節(jié)點i從編號1開始的左子節(jié)點位置在2i,此處數(shù)組下標(biāo)從0開始,即左子節(jié)點所在數(shù)組索引位置為:2i + 1
while (childIndex < length) {
if (childIndex + 1 < length && nums[childIndex] < nums[childIndex + 1]) {
childIndex++; //節(jié)點有右子節(jié)點,且右子節(jié)點大于左子節(jié)點,則選取右子節(jié)點
}
if (temp > nums[childIndex]) {
break; //如果選中節(jié)點大于其子節(jié)點,直接返回
}
nums[parent] = nums[childIndex];
parent = childIndex;
childIndex = 2 * parent + 1; //繼續(xù)向下調(diào)整
}
nums[parent] = temp;
}
}
Python3
#堆排序
def heap_sort(nums):
for i in range(int(len(nums) / 2 - 1), -1, -1):
heap_adjust(nums, i, len(nums))
for i in range(len(nums) - 1, -1, -1):
temp = nums[i]
nums[i] = nums[0]
nums[0] = temp
heap_adjust(nums, 0, i)
return nums
#調(diào)整堆
def heap_adjust(nums, parent, length):
temp = nums[parent]
childIndex = 2 * parent + 1
while childIndex < length:
if childIndex + 1 < length and nums[childIndex] < nums[childIndex + 1]:
childIndex += 1
if temp > nums[childIndex]:
break
nums[parent] = nums[childIndex]
parent = childIndex
childIndex = 2 * parent + 1
nums[parent] = temp
nums = [53, 17, 78, 09, 45, 65, 87, 32]
nums = heap_sort(nums)
print(nums)
以上這篇老生常談比較排序之堆排序就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot使用@Async注解可能會遇到的8大坑點匯總
SpringBoot中,@Async注解可以實現(xiàn)異步線程調(diào)用,用法簡單,體驗舒適,但是你一定碰到過異步調(diào)用不生效的情況,今天,我就列出90%的人都可能會遇到的8大坑點,需要的朋友可以參考下2023-09-09
詳解Java并發(fā)編程之volatile關(guān)鍵字
這篇文章主要為大家介紹了Java并發(fā)編程之volatile關(guān)鍵字,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-11-11
java后臺實現(xiàn)js關(guān)閉本頁面,父頁面指定跳轉(zhuǎn)或刷新操作
這篇文章主要介紹了java后臺實現(xiàn)js關(guān)閉本頁面,父頁面指定跳轉(zhuǎn)或刷新操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Springboot集成Elasticsearch的步驟與相關(guān)功能
ElasticSearch是開源搜索平臺領(lǐng)域的一個新成員,?ElasticSearch是一個基于Lucene構(gòu)建的開源,分布式,RESTful搜索引擎,這篇文章主要給大家介紹了關(guān)于Springboot集成Elasticsearch的相關(guān)資料,需要的朋友可以參考下2021-12-12
java 中遍歷取值異常(Hashtable Enumerator)解決辦法
這篇文章主要介紹了java 中遍歷取值異常(Hashtable Enumerator)解決辦法的相關(guān)資料,用迭代器取值時拋出的異常:java.util.NoSuchElementException: Hashtable Enumerator ,需要的朋友可以參考下2017-08-08
springboot中@ConfigurationProperties無效果的解決方法
本文主要介紹了springboot中@ConfigurationProperties無效果,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06

