lecture03sorts2 - Sorting II QuickSort General lower bound...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Sorting II QuickSort General lower bound for sorting External sorting
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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 )}.
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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) }
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 Weiss textbook quicksort { 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 }
Background image of page 8
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
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 48

lecture03sorts2 - Sorting II QuickSort General lower bound...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online