老生常談比較排序之堆排序
對(duì)于堆排序會(huì)涉及一些完全二叉樹知識(shí)。對(duì)于待排序列{10, 2, 11, 8, 7},把它看成是一顆完全二叉樹,如下圖所示。
堆分為大根堆和小根堆:大根堆表示每個(gè)根節(jié)點(diǎn)均大于其子節(jié)點(diǎn)(L(i) >= L(2i) && L(i) >= L(2i + 1)),小根堆表示每個(gè)根節(jié)點(diǎn)均小于其子節(jié)點(diǎn)(L(i) <= L(2i) && L(i) <= L(2i + 1))。(在完全二叉樹中第i個(gè)節(jié)點(diǎn)的左子節(jié)點(diǎn)為2i,其右字節(jié)點(diǎn)為2i + 1)
本文將以大根堆的構(gòu)建作為示例進(jìn)行講解。
堆排序的第一步——構(gòu)建初始堆。如何構(gòu)建初始堆呢?根據(jù)定義,關(guān)鍵點(diǎn)在于每個(gè)根節(jié)點(diǎn)。觀察上述待排序列的完全二叉樹,不難發(fā)現(xiàn)存在節(jié)點(diǎn)2和節(jié)點(diǎn)10有子節(jié)點(diǎn),它們是需要關(guān)注的節(jié)點(diǎn)。
如何定位節(jié)點(diǎn)2呢?發(fā)現(xiàn)它是葉子節(jié)點(diǎn),或者最后一個(gè)節(jié)點(diǎn)的父節(jié)點(diǎn),根據(jù)完全二叉樹的性質(zhì)可知,除根節(jié)點(diǎn)外任意節(jié)點(diǎn)的父節(jié)點(diǎn)的編號(hào)為⌊n / 2⌋。已知n = 5,易知節(jié)點(diǎn)2的編號(hào)為⌊5 / 2⌋ = ②。比較它與左右子節(jié)點(diǎn)的大小并調(diào)整。
最后剩下根節(jié)點(diǎn)10,已知節(jié)點(diǎn)2的編號(hào)為②,② - 1 = ①即得到根節(jié)點(diǎn)10的編號(hào)。比較它與左右子節(jié)點(diǎn)的大小并調(diào)整。
調(diào)整完畢后發(fā)現(xiàn)已經(jīng)構(gòu)成了一個(gè)“大根堆”,示例中的待排序列較為簡(jiǎn)單,再給出一個(gè)較為復(fù)雜的待排序列,觀察其構(gòu)建大根堆的過程。對(duì)于待排序列{53, 17, 78, 09, 45, 65, 87, 32},將它看成一顆完全二叉樹。
同樣我們來看它所需要關(guān)注的節(jié)點(diǎn)有哪些。
根據(jù)第一個(gè)例子,我們很容易能定位節(jié)點(diǎn)09的編號(hào)為⌊8 / 2⌋ = ④,節(jié)點(diǎn)78的編號(hào)為④ - 1 = ③……,依次類推,發(fā)現(xiàn)了一定的規(guī)律,即需要調(diào)整的節(jié)點(diǎn)位置從⌊n / 2⌋開始依次遞減直到根節(jié)點(diǎn)①結(jié)束(⌊n / 2⌋ ~ 1)?,F(xiàn)在開始調(diào)整。
在第四次調(diào)整結(jié)束后發(fā)現(xiàn)節(jié)點(diǎn)53不滿足大根堆的定義,其右子節(jié)點(diǎn)大于它,此時(shí)需要做進(jìn)一步的向下調(diào)整。
注意向下調(diào)整是每次向上調(diào)整的時(shí)候都需要做的判斷是否需要向下調(diào)整,而不是在所有的向上調(diào)整結(jié)束過后再回過頭來向下調(diào)整。這樣大根堆就建立好了,此時(shí)待排序列數(shù)組情況已經(jīng)發(fā)生了改變:{87, 45, 78, 32, 17, 65, 53, 09}。接下來是如何進(jìn)行排序的問題。將大根堆的根節(jié)點(diǎn)與最后一個(gè)節(jié)點(diǎn)互換,并調(diào)整二叉樹使其仍然滿足大根堆。
可以看到將根節(jié)點(diǎn)與最后一個(gè)節(jié)點(diǎn)呼喚后,待排序列的最大值已經(jīng)放到了數(shù)組的最后一個(gè)位置{……, 87},此時(shí)完成了第一趟排序,但這第一趟排序還沒有結(jié)束,此時(shí)除節(jié)點(diǎn)87外,其余節(jié)點(diǎn)并不滿足大根堆的條件,所以需要對(duì)其余節(jié)點(diǎn)進(jìn)行調(diào)整為大根堆。排序過程不再給出,Java和Python3的代碼實(shí)現(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é)點(diǎn) * @param length 數(shù)組序列長(zhǎng)度 */ private static void heapAdjust(int[] nums, int parent, int length) { int temp = nums[parent]; int childIndex = 2 * parent + 1; //完全二叉樹節(jié)點(diǎn)i從編號(hào)1開始的左子節(jié)點(diǎn)位置在2i,此處數(shù)組下標(biāo)從0開始,即左子節(jié)點(diǎn)所在數(shù)組索引位置為:2i + 1 while (childIndex < length) { if (childIndex + 1 < length && nums[childIndex] < nums[childIndex + 1]) { childIndex++; //節(jié)點(diǎn)有右子節(jié)點(diǎn),且右子節(jié)點(diǎn)大于左子節(jié)點(diǎn),則選取右子節(jié)點(diǎn) } if (temp > nums[childIndex]) { break; //如果選中節(jié)點(diǎn)大于其子節(jié)點(diǎn),直接返回 } 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)
以上這篇老生常談比較排序之堆排序就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot使用@Async注解可能會(huì)遇到的8大坑點(diǎn)匯總
SpringBoot中,@Async注解可以實(shí)現(xiàn)異步線程調(diào)用,用法簡(jiǎn)單,體驗(yàn)舒適,但是你一定碰到過異步調(diào)用不生效的情況,今天,我就列出90%的人都可能會(huì)遇到的8大坑點(diǎn),需要的朋友可以參考下2023-09-09詳解Java并發(fā)編程之volatile關(guān)鍵字
這篇文章主要為大家介紹了Java并發(fā)編程之volatile關(guān)鍵字,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-11-11運(yùn)用springboot搭建并部署web項(xiàng)目的示例
這篇文章主要介紹了運(yùn)用springboot搭建并部署web項(xiàng)目的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06解析MyBatis源碼實(shí)現(xiàn)自定義持久層框架
這篇文章主要介紹了手撕MyBatis源碼實(shí)現(xiàn)自定義持久層框架,涉及到的設(shè)計(jì)模式有Builder構(gòu)建者模式、??模式、代理模式,本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05java后臺(tái)實(shí)現(xiàn)js關(guān)閉本頁面,父頁面指定跳轉(zhuǎn)或刷新操作
這篇文章主要介紹了java后臺(tái)實(shí)現(xiàn)js關(guān)閉本頁面,父頁面指定跳轉(zhuǎn)或刷新操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11Springboot集成Elasticsearch的步驟與相關(guān)功能
ElasticSearch是開源搜索平臺(tái)領(lǐng)域的一個(gè)新成員,?ElasticSearch是一個(gè)基于Lucene構(gòu)建的開源,分布式,RESTful搜索引擎,這篇文章主要給大家介紹了關(guān)于Springboot集成Elasticsearch的相關(guān)資料,需要的朋友可以參考下2021-12-12mvc架構(gòu)實(shí)現(xiàn)商品的購買(二)
這篇文章主要為大家詳細(xì)介紹了mvc架構(gòu)實(shí)現(xiàn)商品購買功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11java 中遍歷取值異常(Hashtable Enumerator)解決辦法
這篇文章主要介紹了java 中遍歷取值異常(Hashtable Enumerator)解決辦法的相關(guān)資料,用迭代器取值時(shí)拋出的異常:java.util.NoSuchElementException: Hashtable Enumerator ,需要的朋友可以參考下2017-08-08springboot中@ConfigurationProperties無效果的解決方法
本文主要介紹了springboot中@ConfigurationProperties無效果,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06