1Computer Sciences
2 HEAP SORT TUTORIAL 4
Objective O(n lg n) worst case like merge sort. Sorts in place like insertion sort. A heap can be stored as an array A. Root of tree is A[1]. Parent of A[i ] = A[i/2]. Left child of A[i ] = A[2i ]. Right child of A[i ] = A[2i + 1]. Computing is fast with binary representation implementation.
Heap data structure Heap is a nearly complete binary tree. “Heap is almost- complete binary tree.” Height of node = # of edges on a longest simple path from the node down to a leaf. Height of heap = height of root = (lg n). Computer Sciences4
Maintaining the heap property Computer Sciences5
Building a heap ( a bottom-up manner ) Computer Sciences6
The heap-sort algorithm Computer Sciences7
Exercises Illustrate the operation of heapSort on the below array: {17, 1, 9, 14, 2, 8, 3} a)b) Computer Sciences8
c)d) Computer Sciences9
e)f) Computer Sciences10
g) h) 9 Computer Sciences11
i) j) 3 Computer Sciences12
k) l) Sorted Computer Sciences13
a)b) Build a maxHeap from the below array: {15, 9, 20, 13, 4, 59} Exercises Computer Sciences14
c) d) Computer Sciences15
e)f) Computer Sciences16
g) h) Computer Sciences17
Computer Sciences18
Computer Sciences19