Download presentation
Presentation is loading. Please wait.
Published byCameron Berry Modified over 8 years ago
1
1Computer Sciences
2
2 HEAP SORT TUTORIAL 4
3
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.
4
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
5
Maintaining the heap property Computer Sciences5
6
Building a heap ( a bottom-up manner ) Computer Sciences6
7
The heap-sort algorithm Computer Sciences7
8
Exercises Illustrate the operation of heapSort on the below array: {17, 1, 9, 14, 2, 8, 3} 17 1 9 14 2 8 3 17 38 2 14 91 6 5 7 4 3 2 1 a)b) Computer Sciences8
9
17 1 9 14 2 8 3 17 3 8 2 1 9 14 c)d) Computer Sciences9
10
17 14 9 1 2 83 17 8 21 9 3 e)f) Computer Sciences10
11
3 8 1 2 1417 8 149 1 2 3 g) h) 9 Computer Sciences11
12
1 2 8 9 14 17 2 1498 3 1 i) j) 3 Computer Sciences12
13
23 89 14 17 k) l) 1 171498321 Sorted Computer Sciences13
14
15 9 20 13 4 59 15 59 4 13 209 6 5 4 3 2 1 a)b) Build a maxHeap from the below array: {15, 9, 20, 13, 4, 59} Exercises Computer Sciences14
15
15 9 59 134 20 15 20 4 13 59 9 c) d) Computer Sciences15
16
13 59 9 4 20 15 20 4 9 59 13 e)f) Computer Sciences16
17
59 13 15 9 4 20 59 15 49 20 13 g) h) Computer Sciences17
18
Computer Sciences18
19
Computer Sciences19
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.