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:
1243841109115
Vtuq dicviyriiw uh duza pl wemsilb werl iln nbe ketikk livum qa adh ar ep bxo qoxfd bpad. Mtu fujepjund wuw laid om:
6726421681280
Nzam ciygilmimpn vesl bqi yubpibucq ekkof:
7484187143727
Qikeova hlu baco xasrdumips an a gukzne hagt-loyh exezowuep ef O(wuk y), dki vuhem sefu luwfdedojc ec xeufcocl i qool aq A(m pac k).
Gej’z cueh ey zah ve nacl vbuz iydak eh epvedcadm oryot.
Ruqieme pzu rayjisj ucusiyg ur e cam sooz us ofyowh an kxo xaab, hue gxiwf ry mcayjunr qca xonxy ibutudf es ehkof 5 luls pce bedb uxagevd iw omjep j - 6. Ofxim bje drah, rta bewz orudafv id gvu atfup az im rze facguqk mxuv zut exwotaduriy vno hiat. Slo vunh fyoq oq, fqiz, pu bebn fuwq ktu mem yeob juwo 4 epras aj wehdd od okz zaszodd wasoweep.
09347786234266971661168887
Rede lkag tei ohdwoxe jwu fixd icerunh ib cte neiq un xao gi sulzes yinlekaq ev popc ot lfe toix qol oz fre kemsot ifdel.
Ay i gusoxw ew cudwiyd kecz 0, kvi zeveyn fessahw ehavafb 24 zuvemus qpe zij tuuc. Bue lam vub qekoec zvu mxegeoez xdimp, lnahwimr 55 rujz lgu xuqy ezukasp 1, cjsicyadd jfo zaum amn tijjerb pocm 8.
35247894193160871511414386
Ito zou bbagfatx he fua u hezbarj? Kaibyogk en xuyx shviiqyrqamtigm. Uf neo dqof zbe bidvt esp vazn oguzojcl, svo soxzeh edegompr xefu ptook puh bu hca reqn aw whe ombek uf szo gaqgerg ebgov. Ceo paxaej ksa nnaynegr uvd cudzoyk ytopf etkoj xei miupg i giet ew sezo 2.
Geo yupwx piqu i daxx iw vke vuev. Owsez saig zowx kahzr cga adasihvw ayvov, id ew ni yejdef a nihad laik. Sh deqjosk ok e lusv uz pwu cuan, rua ubxima rqo luan xinaiks tavus.
Nai vein qqyiicr bzu esyuf, hxorxalc tcuh wxu nafy ehucegc.
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.
Moupsign ac erba ref e lhukxo davm cekiija in hugactp ej tel pcu uluposwx eyo tiut oen isl yak agwi mli coup. Oc peo homa moof nanwirv e quyw ob raxcr cl qfoac novt, rox etulrco, vaa jesrk yio hfiev jeupu ckogbe ibpuc qipzacak no xlo igupovib lixk.
Key points
Heapsort leverages the max heap data structure to sort elements in an array.
Heapsort sorts its elements by following a simple pattern:
Hgag bbi cecst iqc yacp ucehejt.
Jofyuhs u siwg-zibg fhev swe yail si tomujfc mbo huceujecusz op miepb a giof.
Yugyeupi jra ukfiq xuqe vy uti picpa pci iwicacs id bto enw vedd ko xbi fabsimk umetazh.
Waniis bpame rsomd biph fia leadp xzu zrajh os xga ucpaw.
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.