Design and Analysis of Algorithms Dr. Maheswari Karthikeyan 09/03/2013, Lecture 6
HeapSort
HEAPSORT Heap - The heap data structure is an array object which can be viewed as a nearly complete binary tree. - the two attributes of heap are length[A] and heap-size[A] Heap as an ARRAY - given an index i of a node, Parent(i) return (i/2) Left(i) return 2i Right(i) return 2i + 1 17 PARENT LEFT CHILD RIGHT CHILD 5 13
HEAPSORT Max-heap - max-heap property is A[parent(i)] >= A[i] Min-heap - min-heap property is A[parent(i)] <= A[i] max-heap min-heap 17 6 5 13 12 14
HEAPSORT Basic Procedures used in Heapsort algorithm MAX-HEAPIFY – maintains the heap property. BUILD-MAX-HEAP – produces a max-heap from an unordered input array. HEAPSORT – sorts an array in place
HEAPSORT MAX-HEAPIFY (A,i) l left(i) r right(i) if l <= heap-size[A] and A[l] > A[i] then largest l else largset i if r <= heap-size[A] and A[r] > A[largest] then largest r if largest ≠ i then exchange A[i] <-> A[largest] MAX-HEAPIFY (A,largest)
MAX-HEAPIFY MAX-HEAPIFY( A,2) 1 4 3 2 33 1 5 6 7 4 15 16 9 19 10 9 8 14 7 11 MAX-HEAPIFY( A,2)
MAX-HEAPIFY MAX-HEAPIFY( A,4) 1 4 3 2 3 19 5 6 7 4 15 16 9 1 10 9 8 14 11 MAX-HEAPIFY( A,4)
MAX-HEAPIFY MAX-HEAPIFY( A,8) 1 4 3 2 3 19 5 6 7 4 15 16 9 14 10 9 8 1 11 MAX-HEAPIFY( A,8)
MAX-HEAPIFY MAX-HEAPIFY( A,1 ) 1 4 3 2 9 30 5 6 7 4 24 2 8 25 10 9 8 20 12 22 MAX-HEAPIFY( A,1 )
MAX-HEAPIFY MAX-HEAPIFY( A,1 ) 1 30 3 2 9 25 5 6 7 4 24 2 8 22 10 9 8 20 12 4 MAX-HEAPIFY( A,1 )
Building a HEAP BUILD-MAX-HEAP (A) Heap-size[A] length[A] for i length[A]/2 downto 1 do MAX-HEAPIFY (A,i)
Building a HEAP 4 1 3 19 13 16 9 14 7 11 1 4 3 2 3 1 5 6 7 4 13 16 9 19 10 9 8 14 7 11
Building a HEAP 4 1 3 19 15 16 9 14 7 11 MAX-HEAPIFY( A,5) 1 4 3 2 3 1 13 16 9 19 10 9 8 14 7 11 MAX-HEAPIFY( A,5)
Building a HEAP 4 1 3 19 15 16 9 14 7 11 MAX-HEAPIFY( A,4) 1 4 3 2 3 1 13 16 9 19 10 9 8 14 7 11 MAX-HEAPIFY( A,4)
Building a HEAP 4 1 3 19 15 16 9 14 7 11 MAX-HEAPIFY( A,3) 1 4 3 2 3 1 13 16 9 19 10 9 8 14 7 11 MAX-HEAPIFY( A,3)
Building a HEAP 4 1 3 19 15 16 9 14 7 11 MAX-HEAPIFY( A,2) 1 4 3 2 16 13 3 9 19 10 9 8 14 7 11 MAX-HEAPIFY( A,2)
Building a HEAP 4 1 3 19 15 16 9 14 7 11 MAX-HEAPIFY( A,4) 1 4 3 2 16 13 3 9 1 10 9 8 14 7 11 MAX-HEAPIFY( A,4)
Building a HEAP 4 1 3 19 15 16 9 14 7 11 MAX-HEAPIFY( A,1) 1 4 3 2 16 13 3 9 14 10 9 8 1 7 11 MAX-HEAPIFY( A,1)
Building a HEAP 4 1 3 19 15 16 9 14 7 11 MAX-HEAPIFY( A,2) 1 19 3 2 16 13 3 9 14 10 9 8 1 7 11 MAX-HEAPIFY( A,2)
Building a HEAP 4 1 3 19 15 16 9 14 7 11 MAX-HEAPIFY( A,4) 1 19 3 2 16 13 3 9 4 10 9 8 1 7 11 MAX-HEAPIFY( A,4)
Building a HEAP 4 1 3 19 15 16 9 14 7 11 1 19 3 2 16 14 5 6 7 4 13 3 9 7 10 9 8 1 4 11 DONE
Building a HEAP 5 3 17 10 84 19 6 22 9 BUILD a heap on the above array
Building a HEAP 5 3 17 10 84 19 6 22 9 1 84 3 2 19 22 5 6 7 4 3 17 6 10 9 8 5 9
HeapSort algorithm HEAPSORT(A) BUILD-MAX-HEAP(A) for i length[A] downto 2 do exchange A[1] <-> A[i] heap-size [A] heap-size[A] – 1 MAX-HEAPIFY(A,1)
HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 19 3 2 16 14 5 6 7 4 13 3 9 7 10 9 8 1 4 11 A
HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 11 3 2 16 14 5 6 7 4 13 3 9 7 9 8 1 4 A 19
HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 16 3 2 11 14 5 6 7 4 13 3 9 7 9 8 1 4 A 19
HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 4 3 2 11 14 5 6 7 4 13 3 9 7 8 1 A 16 19
HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 14 3 2 11 13 5 6 7 4 4 3 9 7 8 1 A 16 19
19 14 16 7 13 3 9 1 4 11 1 1 3 2 11 13 5 6 7 4 4 3 9 7 A 14 16 19
HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 13 3 2 11 7 5 6 7 4 4 3 9 1 A 14 16 19
HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 13 3 2 11 7 5 6 7 4 4 3 9 1 A 14 16 19
HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 9 3 2 11 7 5 6 4 4 3 1 A 13 14 16 19
HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 11 3 2 9 7 5 6 4 4 3 1 A 13 14 16 19
HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 3 3 2 9 7 5 4 4 1 A 11 13 14 16 19
HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 9 3 2 3 7 5 4 4 1 A 11 13 14 16 19
HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 4 3 2 3 7 4 1 A 09 11 13 14 16 19
HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 7 3 2 3 4 4 1 A 09 11 13 14 16 19
HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 1 3 2 3 4 A 07 09 11 13 14 16 19
HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 4 3 2 3 1 A 07 09 11 13 14 16 19
HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 3 2 1 A 04 07 09 11 13 14 16 19
19 14 16 7 13 3 9 1 4 11 A 01 03 04 07 09 11 13 14 16 19
RUNNING TIME OF HEAPSORT RUNNING TIME OF MAX-HEAPIFY Running time of MAX-HEAPIFY depends on the height h of a heap i.e O(h) Height of a heap = lg n T(n) = O(lg n)
RUNNING TIME OF HEAPSORT HEAPSORT(A) BUILD-MAX-HEAP(A) for i length[A] downto 2 do exchange A[1] <-> A[i] heap-size [A] heap-size[A] – 1 MAX-HEAPIFY(A,1) Running time of BUILD-MAX-HEAP is O(n) MAX-HEAPIFY is executed n-1 times with running time O(log n) Total Running time of Heap Sort is O(n) + n-1(O(log n) = O(n log n)
RUNNING TIME OF HEAPSORT Running time of BULID-MAX-HEAP lg n lg n Total cost of BULID-MAX-HEAP is ∑ n/2 h+1 O(h) = O (n ∑ h/2 h) h=0 h=0 ∞ ∑ h/2 h = ½ = 2. h=0 -------- (1- ½)2 Running time of BUILD-MAX-HEAP can be bounded as lg n ∞ O (n ∑ h/2 h) = O(n ∑ h/2 h) = O (n) . h=0 h=0