10. Heapsort and Priority Queues

Heaps

  1. A binary heap is a nearly complete binary tree without child-parent pointers, usually stored as an array.
  2. Each node (array element) contains a value (key) and perhaps data associated with the key.
  3. The tree is completely filled on all levels except possibly the lowest (bottom) level, which is filled from the left(Array indexes are assigned top to bottom, left to right).

Two types of heaps: the max-heap and the min-heap

For a max-heap: every node other than the root has a key less than or equal to the key in its parent.

For a min-heap: every node other than the root has a key greater than or equal to the key in its parent.

Basic properties of a max-heap:

  1. The key in the root is the largest key.
  2. Given any node in a max-heap, the node and all its descendants form a max-heap, (i.e., the subtree rooted at the node is a maximum heap, too).

Heaps operation

我們常用兩個方式來操作:

  1. Swap up. If a key is greater than the key of its parent, swap them.
  2. Swap down. If a key is less than the key of one of its two children (or a child), swap the key with the larger key of the two children.

MAX-HEAPIFY operation

Make the subtree rooted at i into a max-heap, assuming that the subtrees rooted at LEFT(i) and RIGHT(i) are max-heaps already.
Method: swap down.

4E7BD4B4-5058-46F5-8FD7-1528DE6C75F2.png

Time required: O(logn).
The height of an element is the distance(logn) to the bottom level.
即 O(height)

BUILD-MAX-HEAP operation

Given an unordered array A, turn it into a max-heap.

The idea: fix one element at a time, working from the leaves up to the root. As each element’s turn comes, its children are already fixed.

時間復雜度為O(n)盏浙。

過程

Heapsort

Method: Turn the array into a max-heap. Then repeatedly remove the maximum element of the heap into the proper place in the array.

Time required: <= O(n) + (n - 1)*O(logn) = O(nlogn)


過程

Priority Queues

A priority queue is a data structure for maintaining a set S of elements, each with an associated value called the key.

A max-priority queue supports the following operations.

  1. INSERT(S, x) inserts the element x into the set S, i.e., S ← S ∪ {x}.

  2. MAXIMUM(S) returns the element of S with the largest key.

  3. EXTRACT-MAX(S) removes and returns the element of S with the largest key.
    過程:在寫code時威酒,先將root(a[0])和lastnode(a[last]) exchange, 輸出最后一個node,然后執(zhí)行 heapsize - 1家夺。最后對新的heap進行排位(比較children天试,取大的值和root替換)。
    原因:我們用數(shù)組實現(xiàn)heap。

  4. INCREASE-KEY(S,x,k) increases the value of element x’s key to the new value k, which is assumed to be at least as large as x’s current key value.

We can implement the operations of a max-priority queue S, using a max-heap A.

  1. HEAP-MAXIMUM(A) ---O(1)
  2. HEAP-EXTRACT-MAX(A) --- O(logn) using MAX-HEAPIFY(A, 1)
  3. HEAP-INCREASE-KEY(A, i, key) --- O(logn)
  4. MAX-HEAP-INSERT(A, key) --- O(logn) using HEAP-INCREASE-KEY(A,heap size[A],key)

總結

Heapsort is an in place sorting algorithm that runs in O(nlogn).

  1. It achieves asymptotically optimal running time
  2. It only takes a constant amount of space outside the input array
  3. The heapsort algorithm uses the heap data structure, which can also be used for minimum/maximum priority queues
  4. It is an optimal sorting algorithm
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末腊尚,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子满哪,更是在濱河造成了極大的恐慌婿斥,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,723評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件哨鸭,死亡現(xiàn)場離奇詭異民宿,居然都是意外死亡,警方通過查閱死者的電腦和手機像鸡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,485評論 2 382
  • 文/潘曉璐 我一進店門活鹰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人只估,你說我怎么就攤上這事志群。” “怎么了蛔钙?”我有些...
    開封第一講書人閱讀 152,998評論 0 344
  • 文/不壞的土叔 我叫張陵锌云,是天一觀的道長。 經(jīng)常有香客問我吁脱,道長桑涎,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,323評論 1 279
  • 正文 為了忘掉前任兼贡,我火速辦了婚禮攻冷,結果婚禮上,老公的妹妹穿的比我還像新娘遍希。我一直安慰自己等曼,他們只是感情好,可當我...
    茶點故事閱讀 64,355評論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著涉兽,像睡著了一般招驴。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上枷畏,一...
    開封第一講書人閱讀 49,079評論 1 285
  • 那天别厘,我揣著相機與錄音,去河邊找鬼拥诡。 笑死触趴,一個胖子當著我的面吹牛,可吹牛的內容都是我干的渴肉。 我是一名探鬼主播冗懦,決...
    沈念sama閱讀 38,389評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼仇祭!你這毒婦竟也來了披蕉?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 37,019評論 0 259
  • 序言:老撾萬榮一對情侶失蹤乌奇,失蹤者是張志新(化名)和其女友劉穎没讲,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體礁苗,經(jīng)...
    沈念sama閱讀 43,519評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡爬凑,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 35,971評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了试伙。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片嘁信。...
    茶點故事閱讀 38,100評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖疏叨,靈堂內的尸體忽然破棺而出潘靖,到底是詐尸還是另有隱情,我是刑警寧澤蚤蔓,帶...
    沈念sama閱讀 33,738評論 4 324
  • 正文 年R本政府宣布秘豹,位于F島的核電站,受9級特大地震影響昌粤,放射性物質發(fā)生泄漏。R本人自食惡果不足惜啄刹,卻給世界環(huán)境...
    茶點故事閱讀 39,293評論 3 307
  • 文/蒙蒙 一涮坐、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧誓军,春花似錦袱讹、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,289評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽椒丧。三九已至,卻和暖如春救巷,著一層夾襖步出監(jiān)牢的瞬間壶熏,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,517評論 1 262
  • 我被黑心中介騙來泰國打工浦译, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留棒假,地道東北人。 一個月前我還...
    沈念sama閱讀 45,547評論 2 354
  • 正文 我出身青樓精盅,卻偏偏與公主長得像帽哑,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子叹俏,可洞房花燭夜當晚...
    茶點故事閱讀 42,834評論 2 345

推薦閱讀更多精彩內容