import random
def randomList(count:int=10) -> list:
'''return a random list with elements from 0 to count-1'''
return random.sample(range(count), count)
# Recursively. Θ(lgn)
def maxHeapify(array, arrayLength, fatherIndex):
'''Maintain the max-heap property from the fatherIndex-th element'''
largestIndex = fatherIndex
leftIndex = 2 * fatherIndex
rightIndex = leftIndex+1
# Keep indices lower than len of the array
if leftIndex<arrayLength and array[leftIndex]>array[largestIndex]:
largestIndex = leftIndex
if rightIndex<arrayLength and array[rightIndex]>array[largestIndex]:
largestIndex = rightIndex
if largestIndex != fatherIndex:
array[largestIndex], array[fatherIndex] = array[fatherIndex], array[largestIndex]
maxHeapify(array, arrayLength, largestIndex)
# Cyclically. Θ(lgn)
def maxHeapify(array, arrayLength, fatherIndex):
'''Maintain the max-heap property from the fatherIndex-th element'''
largestIndex = fatherIndex
while True:
leftIndex = 2 * fatherIndex
rightIndex = leftIndex+1
# Keep indices lower than len of the array
if leftIndex<arrayLength and array[leftIndex]>array[largestIndex]:
largestIndex = leftIndex
if rightIndex<arrayLength and array[rightIndex]>array[largestIndex]:
largestIndex = rightIndex
if largestIndex != fatherIndex:
array[largestIndex], array[fatherIndex] = array[fatherIndex], array[largestIndex]
fatherIndex = largestIndex
else:break
# Θ(n)
def buildMaxHeap(array):
'''Convert an original array into a max heap'''
for i in reversed(range(len(array)//2)):
maxHeapify(array, len(array), i)
# Θ(nlgn)
def heapSort(array):
'''Heap sort a designated array'''
if len(array)<=1:
return
buildMaxHeap(array)
for i in reversed(range(1, len(array))):
array[0], array[i] = array[i], array[0]
maxHeapify(array, i, 0)
# Trial
array = randomList()
print(array)
heapSort(array)
print(array)
Heap Sort. Θ(nlgn)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門乳怎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來彩郊,“玉大人,你說我怎么就攤上這事蚪缀★牛” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵询枚,是天一觀的道長违帆。 經(jīng)常有香客問我,道長金蜀,這世上最難降的妖魔是什么刷后? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮渊抄,結(jié)果婚禮上尝胆,老公的妹妹穿的比我還像新娘。我一直安慰自己护桦,他們只是感情好含衔,可當(dāng)我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著二庵,像睡著了一般贪染。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上催享,一...
- 文/蒼蘭香墨 我猛地睜開眼炬称,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了涡拘?” 一聲冷哼從身側(cè)響起玲躯,我...
- 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎鳄乏,沒想到半個月后跷车,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡橱野,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年朽缴,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片水援。...
- 正文 年R本政府宣布奕扣,位于F島的核電站薪鹦,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏惯豆。R本人自食惡果不足惜池磁,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望楷兽。 院中可真熱鬧地熄,春花似錦、人聲如沸芯杀。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽瘪匿。三九已至,卻和暖如春寻馏,著一層夾襖步出監(jiān)牢的瞬間棋弥,已是汗流浹背。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長得像粉寞,于是被迫代替她去往敵國和親尼荆。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 突然發(fā)現(xiàn)自己對這三種排序算法一無所知唧垦。他們是基于不比較類型的算法捅儒。在特殊情況下,時間復(fù)雜度可以達(dá)到 O(n) 看下...
- 堆排序是一種樹形選擇排序振亮,是對直接選擇排序的有效改進(jìn)巧还。 基本思想: 堆頂元素(即第一個元素)必為最小項(xiàng)(小頂堆)或...
- 堆排序算法,基于選擇排序的思想坊秸,利用堆結(jié)構(gòu)的性質(zhì)來完成對數(shù)據(jù)的排序麸祷。 前提準(zhǔn)備: 什么是堆結(jié)構(gòu):堆數(shù)據(jù)結(jié)構(gòu)是一種數(shù)...