CE 221 Data Structures and Algorithms Chapter 7: Sorting (Heapsort, Mergesort) Text: Read Weiss, § 7.5 – 7.6 Izmir University of Economics 1
Heapsort • Priority queues can be used sort in O ( N log N ) time. – First, build a binary heap of N elements in O ( N ) time – perform N deleteMin operations each taking O (log N ) time, hence resulting in O ( N log N ). • Problem : needs an extra array. • A clever solution : after each deleteMin heap size shrinks by 1, use this space. But the result will be a decreasing sorted order. Thus we may use a max heap by changing the heap-order property Izmir University of Economics 2
Heapsort Example Max heap after buildHeap phase Max heap after first deleteMax phase Izmir University of Economics 3
Analysis of Heapsort N 2 log i 2 log N ! 2 N log N O ( N ), • worst case analysis i 2 • 2N comparisons- buildHeap N N • N-1 deleteMax operations. ( ! 2 ) N N e • Theorem : Average # of comparisons to heapsort a random permutation of N distinct items is 2NlogN-O(NloglogN). • Proof : on any input, cost sequence D: d 1 , d 2 ,..., d N N Cost M d , number of comparison s 2 M D i D i 1 N f ( N ) ( N /( 4 e )) number of heaps with N items d d d M 1 2 N D for any D, distinct deleteMax sequences S 2 2 ... 2 S 2 D D M 1 total # of heaps with cost less than M is at most N i N M (log N ) 2 (log N ) 2 # of heaps with cost M < N(logN-loglogN-4) i 1 is at most (N/16) N . So the average # of comparisons is at least 2M. Izmir University of Economics 4
Mergesort • runs in O ( N log N ), # of comparisons is nearly optimal, fine example of a recursive algorithm. • Fundamental operation is merging of 2 sorted lists. • The basic merging algorithm takes 2 input arrays A and B , an output array C . 3 counters, Actr , Bctr , and Cctr are initially set to the beginning of their respective arrays. • The smaller of A[ Actr ] and B[ Bctr ] is copied to C[ Cctr ] and the appropriate counters are advanced. • When either list is exhausted, the rest of the other list is copied to C. Izmir University of Economics 5
Mergesort - merge • The time to merge is linear, at most N-1 comparisons are required ( since each comparison adds an element to C. It should also be noted that after N-1 elements are added to array C, the last element need not be compared but it simply gets copied ). • Mergesort algorithm is then easy to describe. If N=1 DONE else { mergesort(left half); mergesort(right half); merge(left half, right half); } Izmir University of Economics 6
Mergesort – Implementation - I Izmir University of Economics 7
Merge – Implementation - II /* leftPos = start of left half, rightPos = start of right half Last copying may be avoided by interchanging the roles of the arrays a and tmpArray at alternate levels of recursion */ Izmir University of Economics 8
Analysis of Mergesort • Write down recurrence relations and solve them – T(1)=1 – T(N)=2T(N/2)+N // assumption is N=2 k T ( N ) T ( N / 2 ) 1 N N / 2 T ( N / 2 ) T ( N / 4 ) 1 N / 2 N / 4 T ( N ) 2 T ( N / 2 ) N T ( N / 4 ) T ( N / 8 ) T ( N ) 4 T ( N / 4 ) 2 N 1 N / 4 N / 8 T ( N ) 8 T ( N / 8 ) 3 N T ( 2 ) T ( 1 ) 1 k k T ( N ) 2 T ( N / 2 ) kN , use k log N 2 1 T ( N ) NT ( 1 ) N log N .......... .......... .......... ..... T ( N ) N N log N T ( N ) T ( 1 ) log N N 1 T ( N ) N log N N O ( N log N ) Izmir University of Economics 9
Homework Assignments • 7.11, 7.12, 7.15, 7.17, 7.43 (7.38 in 3/e), 7.53 (7.48 in 3/e) • You are requested to study and solve the exercises. Note that these are for you to practice only. You are not to deliver the results to me. Izmir University of Economics 10
Recommend
More recommend