Download presentation
Presentation is loading. Please wait.
1
Sorting (Heapsort, Mergesort)
CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.5 – 7.6 Izmir University of Economics 1
2
Izmir University of Economics
Heapsort Priority queues can be used to sort in O(NlogN) time. First, build a binary heap of N elements in O(N) time perform N deleteMin operations each taking O(logN) time, hence resulting in O(N logN). 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
3
Izmir University of Economics
Heapsort Example Max heap after buildHeap phase Max heap after first deleteMax phase Izmir University of Economics
4
Izmir University of Economics
Analysis of Heapsort worst case analysis 2N comparisons-buildHeap N-1 deleteMax operations. Theorem: Average # of comparisons to heapsort a random permutation of N distinct items is 2NlogN-O(NloglogN). Proof: on any input, cost sequence D: d1, d2,..., dN for any D, distinct deleteMax sequences total # of heaps with cost less than M is at most # of heaps with cost M < N(logN-loglogN-4) is at most (N/16)N. So the average # of comparisons is at least 2M. Izmir University of Economics
5
Izmir University of Economics
Mergesort runs in O(NlogN), # 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
6
Izmir University of Economics
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
7
Mergesort - Implementation
void Mergesort( ElementType A[ ], int N ) { ElementType *TmpArray; TmpArray = malloc( N * sizeof( ElementType ) ); if( TmpArray != NULL ) MSort( A, TmpArray, 0, N - 1 ); free( TmpArray ); } else FatalError( "No space for tmp array!!!" ); Izmir University of Economics
8
MSort - Implementation
void MSort( ElementType A[ ], ElementType TmpArray[ ], int Left, int Right ) { int Center; if( Left < Right ) Center = ( Left + Right ) / 2; MSort( A, TmpArray, Left, Center ); MSort( A, TmpArray, Center + 1, Right ); Merge( A, TmpArray, Left, Center + 1, Right ); } Izmir University of Economics
9
Merge - Implementation
/*Lpos = start of left half, Rpos = start of right half*/ void Merge( ElementType A[ ], ElementType TmpArray[ ], int Lpos, int Rpos, int RightEnd ) { int i, LeftEnd, NumElements, TmpPos; LeftEnd = Rpos - 1; TmpPos = Lpos; NumElements = RightEnd - Lpos + 1; while( Lpos <= LeftEnd && Rpos <= RightEnd ) /*main loop*/ if( A[ Lpos ] <= A[ Rpos ] ) TmpArray[ TmpPos++ ] = A[ Lpos++ ]; else TmpArray[ TmpPos++ ] = A[ Rpos++ ]; while( Lpos <= LeftEnd ) /*Copy rest of first half*/ while( Rpos <= RightEnd ) /*Copy rest of second half*/ for( i = 0; i < NumElements; i++, RightEnd-- ) /*Copy TmpArray back*/ A[ RightEnd ] = TmpArray[ RightEnd ]; } /* Last copying may be avoided by interchanging the roles of A and TmpArray at alternate levels of recursion*/ Izmir University of Economics
10
Izmir University of Economics
Analysis of Mergesort Write down recurrence relations and solve them T(1)=1 T(N)=2T(N/2)+N // assumption is N=2k Izmir University of Economics
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.