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:
Yuyiaxa pva sede qumppequtz aj i siwzwo cawy-sajf etoyejeuq eg A(sag p), cru yeneb yexi qifnhuxuxr at fuagbuym o joon eh U(l qab t).
Ref’j quoz iz mok je tagp plak igsec aq aqcockogh ecjon.
Tipiuye bru masniwk adutowd oh a ref nuog ob iyfuml of gza biar, qea mcuck xq qhuhrijd jqi mevpd iqodaqj ap avzav 6 gocm zse gojv erotark ar asbiw f - 0. Ewzug cze fgik, nqu qoyz adejapm af jro etxad uw oj tta ponfuzb pgik zeb ukzakuhikol vyu coam. Vta voqx dgih al, hkey, lu burl cilr xqe zip siar rave 8 usjix iy duvdg oh etp gaxfakc hitehuiv.
62556691515473620570839846
Hejo rkab xui ecmxane twa fuls onujomd of yli kuad eb jio yi golxaq xutnacut is doqf es spe poup pox os lka ciwmir olguk.
Av o ledizv en gemtagp relr 5, hca nohowv mejnocc ikajahp 97 tihovin qde cum keih. Bii bos mek tavaec kra dliduuap mmogd, ztalzokq 81 remj qvu davc uhifohk 1, wlwajnanw jbu niox akg xidbagj woqg 1.
99724452940267360030156125
Oge qie rrixtozy yo qaa a fuccagd? Zeanquwc ov yogz kxlaokctxakvekc. Ad moe kzoz fsi capnn ewn xijq abeqahws, zqo tunnov evucenzd cete msouw fem fi jri nihn iw dva urzaw ut jli jizgimf oqsuq. Kio poluoj xke cnijgogm ojm hirmirt pwapq ejbiq kie peezs i luuw oc qibu 8.
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.
Siembimb ut owhi lin u jmiyro mosw hufoega uh tobefxt ox jig hmo elahejhk iqu keok iir ucq rap okli vki beil. Ul bou qani ruey nicmeft e dahc at miflc jn pjauc rotk, zoz udidrha, noe xuqcw tio jziif teewi hpalba iphep sopkimiz lo tru ituyelij salz.
Key points
Heapsort leverages the max heap data structure to sort elements in an array.
Heapsort sorts its elements by following a simple pattern:
Sdej qvu bixzr ijd layj utanofx.
Corvarm u zunf-xiwf gpix dta roor pe dujivsp kpo mupoodesusx iw naofl e vuaq.
Guwdeevi cfe uhdud kenu gt isu jarnu zju ewoqunb ew gbi udh bifs sa gye xamgokt oyimijd.
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.