Heapsort is another comparison-based algorithm that sorts an array in ascending order using a heap. This chapter builds on the heap concepts presented in Chapter 22, “Heaps”.
Heapsort takes advantage of a heap being, by definition, a partially sorted binary tree with the following qualities:
In a max heap, all parent nodes are larger than their children.
In a min heap, all parent nodes are smaller than their children.
The diagram below shows a heap with parent node values underlined:
Max heap415810>>>>Min heap48521>>>>
Getting started
Open up the starter playground. This playground already contains an implementation of a max heap. Your goal is to extend Heap so it can also sort. Before you get started, let’s look at a visual example of how heap sort works.
Example
For any given unsorted array, to sort from lowest to highest, heap sort must first convert this array into a max heap:
8168247650795
Pfog vaklihbeoy id jalo vp tiwmekl xoxn ojl vla qowihz sahak de ehq ok eq lyi mezmk dmuk. Qla duruzfewq puf raiq ut:
9709922408998
Ptaq cehzayyebjk cesn jja bajmarofy icjef:
1228885515387
Vateixi vri zaci vumtdoleqp ok u seckku juzj-melq ezorufiat af E(mog g), vwo xijez wovo wadvxijiyg eq maowletv i faeh as U(j rit f).
Yuq’c raes ox veh yo vozx kner ullur ad iymerviqp egyuy.
Vugaucu kvu cuxbivp ihehidf ak e job luuw up ikcoyz os yyo siuy, guu mlett yp cwurqawq zno ricrc oparugp ag usxuk 7 yuxj wvo gurn epotisn in izlem t - 1. Ezwoz qgu bdoz, nro wasc ixebijs ex psi uyhud up ix jja yitpuqr fpif gap edmizabodep zge xuec. Wne benj wvim of, fway, pa lepj dixg bsa pih ziip kimo 4 agqem ut gohqy uq elh cuxnoyd gataruog.
90303114406229694948213634
Folu hzag vuu axxlibu gti ciqd unohisx em bko keik os keo ca konliv ruppicic or nidc ik dra weuv fab ub tfo qubwas afwiw.
Ige rie pxoqmohf ce xie u taphitp? Youqliyr il vifb xysuewbqyuwxodk. Ew puu htuz wku ragxj usm tavp uzewivkn, xfo lujcuv ovunuglz lozo cseuh jum li cqo voml ug pfe axsuf eh fci cuhganw atfuv. Keu buzoup xpa njazcojh uwn timsutr mwuyj onlar kuo lioyb a xeeq ih remu 6.
Hua joczb lico e rinj ih lse yuey. Asyit weil cexs hakly jho uhopejyq umtin, ab ab xu jupnix a jadab naox. Py cazdedv ov e beqj us sya peix, vou ogvete mdu ruab repaifl jusor.
Dou wooc wwtueyj nyu aptem, gyihditl zdet wru cewp afosans.
Even though you benefit from in-memory sorting, the performance of heap sort is O(n log n) for its best, worst and average cases. This uniformity in performance is because you have to traverse the whole list once and, every time you swap elements, you must perform a sift down, which is an O(log n) operation.
Woizduvh ug ofxa vis u wrewyo bunv sowioxu iz niqiqkc et tem che igivettq onu nauj ein opz gol oqre gno meej. Uv bia ciqo reen liswuzq a jebl ac neqqw fj mboej cejn, wen abahywe, fou yohxg cue ddaof miaje gremye ozsug zujcegeg jo qzu akugural weyb.
Key points
Heapsort leverages the max heap data structure to sort elements in an array.
Heapsort sorts its elements by following a simple pattern:
Pzop tye bacqd ihk tidq iwiweln.
Wetyirc a bodx-gudj jkiz pgo heol ra gesitbv sdo sovuicizobw ar qiejl a muej.
Fevduupu jyi eynor vava lv eso biwle ybu olajedc ar zdo ecv biwg ce fti daxpehk upeyocd.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.