{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture03sorts2

# lecture03sorts2 - Sorting II QuickSort General lower bound...

This preview shows pages 1–11. Sign up to view the full content.

1 Sorting II QuickSort General lower bound for sorting External sorting

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 QUICKSORT The fastest known sorting algorithm Divide-and-conquer algorithm Basic algorithm to sort an array S (1) Choose a pivot (2) partition into low and high sub arrays (3) recurse on low and high sub arrays
3 Example: see code example { 13, 81, 92, 43, 65, 31, 57, 26, 75, 0} Assume PIVOT chosen as 65 Partition the data using the PIVOT { 13, 0, 26, 43, 57, 31} 65 { 92, 75, 81} Input array in random order Keys <= PIVOT, to be sorted more Keys >= PIVOT, to be sorted more PIVOT 65 will not be moved by further sorting operations

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 QuickSort 1. If | S |=0 or 1, then return 2. Pick any v in S , which is called the pivot . 3. Partition S -{ v } into two disjoint groups: S 1 with elements <= v , S 2 with elements >= v . 4. Return {quicksort( S 1 ) followed by v followed by quicksort( S 2 )}.
5 Clever operations Choose a good PIVOT in O(1) Partition entire array using PIVOT in O(N) When N is small, use insertion sort instead of recursing

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 QuickSort quicksort ( A, left, right ) { if (left + 10 > right) { insertionSort ( A, left, right ); return; } choose PIVOT as median of A[left], A[middle], A[right]; swap PIVOT and A[right]; // keep PIVOT out of way sweep I from left to right and sweep J from right-1 to left If ( A[J] < PIVOT < A[I] ) swap A[I] and A[j] until I >= J swap A[I] and A[right] to get PIVOT in final place; quicksort ( A, left, I-1) quicksort ( A, I+1, right) }
7 Comments on Weiss quicksort Highly optimized, hard to change Partition step depends on A[left]<=PIVOT Partition step depends on A[right]>=PIVOT Partition yields balanced subarrays even when all keys = PIVOT Ordered array is an easy case with no swaps; 0 swaps for inorder, many for reverse order

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
8 Weiss textbook quicksort void quicksort( vector<Comparable> & a, int left, int right ) { if( left + 10 <= right ) { Comparable pivot = median3( a, left, right ); // O(1) pivot choice int i = left, j = right - 1; // Begin partitioning, an O(N) alg. for( ; ; ) { while( a[ ++i ] < pivot ) { } while( pivot < a[ --j ] ) { } if( i < j ) swap( a[ i ], a[ j ] ); else break; } swap( a[ i ], a[ right - 1 ] ); // Restore pivot quicksort( a, left, i - 1 ); // Sort small elements quicksort( a, i + 1, right ); // Sort large elements } else insertionSort( a, left, right ); // Do an insertion sort on the subarray }
9 Remove recursion by using stack Stack contains sort subproblems to do < (l1,r1)(l2,r2)(l3,r3) … (lK,rK) > Start sorting with < (1, N) > on top Pop and solve subproblems until empty Solving one subproblem may result in two more subproblems being pushed on top

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
10 Quicksort A[1:N] using stack Push sort problem (1,N) on stack While stack is not empty do pop top sort problem (left, right)
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 48

lecture03sorts2 - Sorting II QuickSort General lower bound...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online