sorting: advanced techniques smt genap 2011-2012
TRANSCRIPT
Sorting: Advanced TechniquesSorting: Advanced Techniques
Smt Genap 2011-2012
OutlineOutlineDivide-and-conquer sorting
algorithms:◦Mergesort◦Quicksort
For each algorithm:◦Idea◦Example◦Implementation◦Running time for each algorithm
Smt Genap 2011-2012
Mergesort: Basic IdeaMergesort: Basic IdeaDivide and Conquer approachIdea:
◦Merging two sorted array takes O(n) time◦Split an array into two takes O(1) time
Smt Genap 2011-2012
1 2 3 40 43 65 -1 0 3 4 42 58
-1 1 0 2 3 3 4 40 42 43 58 65
CounterA CounterB
Counterc
Mergesort: Merge ImplementationMergesort: Merge ImplementationImplement operation to merge two
sorted arrays into one sorted array: void mergeSort( AnyType [ ] a, AnyType [ ] tmpArray, int left, int right )
{ if( left < right ) { int center = ( left + right ) / 2; mergeSort( a, tmpArray, left, center ); mergeSort( a, tmpArray, center + 1, right ); merge( a, tmpArray, left, center + 1, right ); } }
Smt Genap 2011-2012
Assume A and B are sorted and |C| = |A| + |B|
Merge RoutinesMerge Routines void merge( AnyType [ ] a, AnyType [ ] tmpArray,
int leftPos, int rightPos, int rightEnd )
{
int leftEnd = rightPos - 1;
int tmpPos = leftPos;
int numElements = rightEnd - leftPos + 1;
while( leftPos <= leftEnd && rightPos <= rightEnd )
if( a[ leftPos ].compareTo( a[ rightPos ] ) <= 0 )
tmpArray[ tmpPos++ ] = a[ leftPos++ ];
else
tmpArray[ tmpPos++ ] = a[ rightPos++ ];
while( leftPos <= leftEnd ) // Copy rest of first half
tmpArray[ tmpPos++ ] = a[ leftPos++ ];
while( rightPos <= rightEnd ) // Copy rest of right half
tmpArray[ tmpPos++ ] = a[ rightPos++ ];
for( int i = 0; i < numElements; i++, rightEnd-- )
a[ rightEnd ] = tmpArray[ rightEnd ];
}
Smt Genap 2011-2012
Mergesort: AlgorithmMergesort: Algorithm
1. If the number of items to sort is 0 or 1, return.2. Recursively sort the first and second half
separately.3. Merge the two sorted halves into a sorted
group.
Smt Genap 2011-2012
Mergesort: ExampleMergesort: Example
Smt Genap 2011-2012
40 2 1 43 3 65 0 -1 58 3 42 4
40 2 1 43 3 65 0 -1 58 3 42 4
40 2 1 43 3 65 0 -1 58 3 42 4
40 2 1 43 3 65 0 -1 58 3 42 4
split
Mergesort: ExampleMergesort: Example
Smt Genap 2011-2012
40 2 1 43 3 65 0 -1 58 3 42 4
2 1 3 65 -1 58 42 4
40 1 2 43 3 65 0 -1 58 3 4 42
split
merge
1 2 40 3 43 65 -1 0 58 3 4 42
Mergesort: Example Mergesort: Example
Smt Genap 2011-2012
merge
1 2 40 3 43 65 -1 0 58 3 4 42
1 2 3 40 43 65 -1 0 3 4 42 58
-1 0 1 2 3 3 4 40 42 43 58 62
Mergesort: ImplementationMergesort: ImplementationMergeSort implementation (and ‘driver’
method)void mergeSort(int[] array){mergeSort(array, 0, a.length-1);}
void mergeSort(int[] a, int left, int right){ if(left < right) { int centre = (left + right)/2; mergeSort(a, left, centre); mergeSort(a, center+1, right); merge(a, left, center+1, right); }}
Smt Genap 2011-2012
How to merge the two subarrays of A without any “temporary” space?
Mergesort: Merge ImplementationMergesort: Merge ImplementationImplement operation to merge two
sorted subarrays:public static void merge(int[] A, int l, int c, int r){
}
Smt Genap 2011-2012
Mergesort: AnalysisMergesort: Analysis
Running Time: O(n log n)Why?
Smt Genap 2011-2012
Quicksort: Basic IdeaQuicksort: Basic Idea
Divide and Conquer approachQuicksort(S) algorithm:
◦If the number of items in S is 0 or 1, return.
◦Pick any element v S. This element is called the pivot.
◦Partition S – {v} into two disjoint groups: L = {x S – {v} | x v} and R = {x S – {v} | x v}
◦Return the result of Quicksort(L), followed by v, followed by Quicksort(R).
Smt Genap 2011-2012
Quicksort: Select Pivot Quicksort: Select Pivot
Smt Genap 2011-2012
40
2
1
3 43
65
0
-158
3
42
4
Quicksort: Partition Quicksort: Partition
Smt Genap 2011-2012
2
1
343
65
0
-1 583
42
440
Quicksort: Recursive Sort & Quicksort: Recursive Sort & MergeMerge
Smt Genap 2011-2012
21 3 43 650-1 583 424 40
21 3 43 650-1 583 424 40
Quicksort: Partition Algorithm 1Quicksort: Partition Algorithm 1
Smt Genap 2011-2012
402 1 43 3 65 0 -1 58 3 424
402 1 433 65 0 -1 58 3 424
40 2 1 43 3 65 0 -1 58 3 42 4
left
40
right
32 1 433 65 0 -1 58 40 424
Quicksort: Partition Algorithm 1Quicksort: Partition Algorithm 1
Smt Genap 2011-2012
402 1 433 650 -1 583 424
402 1 433 65-1 0 583 424
402 1 433 65-1 0 583 424
left right
Quicksort: Partition Algorithm 1Quicksort: Partition Algorithm 1
Smt Genap 2011-2012
-1 3210 3 4 40 42 43 58 65
-1 21 33 42
21 30-1 3 4342 65
2 1 30 -13 4 42 655843
40 2 1 43 3 65 0 -1 58 3 42 4
402 1 433 650-1 583 424
332
32
3
Quicksort: Partition Algorithm 2Quicksort: Partition Algorithm 2
Smt Genap 2011-2012
40 2 1 43 3 65 0 -1 58 3 42 440original
pivot = 40
while < pivot left++ while >= pivot right--
4 2 1 43 3 65 0 -1 58 3 42 40
402 1 43 3 65 0 -1 58 3 424
rightleft
402 1 3 3 65 0 -1 58 43 424
rightleft
“move pivot out of the way”
Quicksort: Partition Algorithm 2Quicksort: Partition Algorithm 2
Smt Genap 2011-2012
402 1 3 3 65 0 -1 58 43 424
rightleft
402 1 3 3 -1 0 65 58 43 424
652 1 3 3 -1 0 40 58 43 424
right leftCROSSING:
Quicksort recursively
“move pivot back”
Quicksort recursively
Quicksort: ImplementationQuicksort: Implementationstatic void QuickSort(int a[], int low, int high) { if(high <= low) return; // base case pivot = choosePivot(a); // select “best” pivot
int i=low, j=high-1; swap(a,pivot,a[j]); // move pivot out of the way while(i <= j) { // find large element starting from left while(i<high && a[i]<pivot) i++;
// find small element starting from right while(j>low && a[j]>=pivot) j--;
// if the indexes have not crossed, swap if(i>j) swap(a, i, j); } swap(a,i,high-1); // restore pivot quickSort(a,low,i-1); // sort small elements quickSort(a,i+1,high); // sort large elements}
Smt Genap 2011-2012
Quicksort: AnalysisQuicksort: Analysis
Partitioning takes◦O(n)
Merging takes◦O(1)
So, for each recursive call, the algorithm takes O(n)
How many recursive calls does a quick sort need?
Smt Genap 2011-2012
Quicksort: Choosing The PivotQuicksort: Choosing The Pivot
Ideal pivot:◦Median element
Common pivot◦First element◦Element at the middle◦Median of three
Smt Genap 2011-2012
Further ReadingFurther ReadingChapter 8: Sorting Algorithm
Smt Genap 2011-2012