MIT OpenCourseWare http://ocw.mit.edu 6.006 Introduction to Algorithms Spring 2008 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Lecture 9 Sorting II: Heaps Lecture 9: Sorting II: Heaps Lecture Overview • Review: Heaps and MAX HEAPIFY • Building a Heap • Heap Sort • Priority Queues (Recitation) Readings CLRS 6.1-6.4 Review Heaps: Parent(i) = �i/2� Left(i) = 2i Right(i) = 2i + 1 Max heap property: A[Parent(i)] ≥ A[i] • MAX HEAPIFY(A, 2) heap size(A) = 10 A[2] ←→ A[4] • MAX HEAPIFY(A,4) A[4] ←→ A[9] 1 6.006 Spring 2008 Lecture 9 Sorting II: Heaps 6.006 Spring 2008 1 16 2 4 8 10 4 6 14 9 2 3 5 8 7 1 7 3 9 10 Violation 1 2 3 4 5 6 16 4 10 14 7 9 7 8 9 10 3 2 8 1 etc O(lg n) time Figure 1: Review from last lecture Building a Heap A[1 · · · n] converted to a max heap Observation: Elements A[�n/2 + 1� · · · n] are all leaves of the tree and can’t have children. BUILD MAX HEAP(A): heap size(A) = length(A) O(n) times for i ← � length[A]/2� downto 1 O(lg n) time do MAX HEAPIFY(A, i) O(n lg n) overall See Figure 2 for an example. 2 Lecture 9 Sorting II: Heaps 1 A 4 2 4 8 2 3 1 5 9 14 3 8 6 16 7 7 10 1 MAX-HEAPIFY (A,3) Swap A[3] and A[7] 4 2 4 8 14 9 2 3 3 1 5 8 6 16 7 7 10 9 10 1 MAX-HEAPIFY (A,2) Swap A[2] and A[5] Swap A[5] and A[10] 4 2 4 8 14 9 2 3 10 1 5 8 6 16 7 4 1 3 2 16 9 10 14 8 7 MAX-HEAPIFY (A,5) no change MAX-HEAPIFY (A,4) Swap A[4] and A[8] 10 9 7 3 9 10 1 4 3 2 8 2 14 9 5 8 MAX-HEAPIFY (A,1) Swap A[1] with A[2] Swap A[2] with A[4] Swap A[4] with A[9] 10 16 4 7 1 6.006 Spring 2008 6 7 9 3 10 Figure 2: Example: Building Heaps 3 Lecture 9 Sorting II: Heaps 6.006 Spring 2008 Sorting Strategy • Build max heap from unordered array • Find maximum element (A[1]) • Put it in correct position A[n], A[n] goes to A[1] New root could violate max heap property but children remain max heaps. • Discard node n from heap (decrement heapsize) Heap Sort Algorithm O(n lg n) BUILD MAX HEAP(A): n times for i =length[A] downto 2 do exchange A[1] ←→ A[i] heap size[A] = heap size[A] − 1 O(lg n) MAX HEAPIFY(A, 1) O(n lg n) overall See Figure 3 for an illustration. 4 Lecture 9 Sorting II: Heaps 6.006 Spring 2008 1 1 16 3 2 10 14 4 8 6 8 5 9 2 4 7 1 3 10 14 3 4 8 10 1 2 7 9 heap_size = 9 MAX_HEAPIFY (A,1) 6 8 5 9 2 4 7 7 3 9 10 16 not part of heap Note: cannot run MAX_HEAPIFY with heapsize of 10 1 1 14 2 4 8 4 2 10 8 5 9 1 3 6 7 2 7 3 9 4 8 1 3 10 8 6 4 5 7 9 2 14 7 3 9 10 not part of heap 16 MAX_HEAPIFY (A,1) 1 1 10 2 4 8 4 2 3 9 8 5 7 6 1 2 7 3 4 4 2 3 5 8 10 9 8 7 6 9 10 14 16 1 MAX_HEAPIFY (A,1) and so on . . . Figure 3: Illustration: Heap Sort Algorithm 5 7 3 not part of heap Lecture 9 Sorting II: Heaps 6.006 Spring 2008 Priority Queues This is an abstract datatype as it can be implemented in different ways. INSERT(S, X) : MAXIMUM(S): EXTRACT MAX(S): INCREASE KEY(S, x, k): inserts X into set S returns element of S with largest key removes and returns element with largest key increases the value of element x’s key to new value k (assumed to be as large as current value) 6