Sorting: Advanced Techniques Smt Genap
Outline Divide-and-conquer sorting algorithms: ◦ Mergesort ◦ Quicksort For each algorithm: ◦ Idea ◦ Example ◦ Implementation ◦ Running time for each algorithm Smt Genap
Mergesort: Basic Idea Divide and Conquer approach Idea: ◦ Merging two sorted array takes O(n) time ◦ Split an array into two takes O(1) time Smt Genap Counter A Counter B Counter c
Mergesort: Merge Implementation Implement 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 Assume A and B are sorted and |C| = |A| + |B|
Merge 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
Mergesort: 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
Mergesort: Example Smt Genap split
Mergesort: Example Smt Genap split merge
Mergesort: Example Smt Genap merge
Mergesort: Implementation MergeSort 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 How to merge the two subarrays of A without any “temporary” space?
Mergesort: Merge Implementation Implement operation to merge two sorted subarrays: public static void merge(int[] A, int l, int c, int r) { } Smt Genap
Mergesort: Analysis Running Time: O(n log n) Why? Smt Genap
Quicksort: Basic Idea Divide and Conquer approach Quicksort(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
Quicksort: Select Pivot Smt Genap
Quicksort: Partition Smt Genap
Quicksort: Recursive Sort & Merge Smt Genap
Quicksort: Partition Algorithm 1 Smt Genap left 40 right
Quicksort: Partition Algorithm 1 Smt Genap leftright
Quicksort: Partition Algorithm 1 Smt Genap
Quicksort: Partition Algorithm 2 Smt Genap original pivot = 40 while < pivot left++ while >= pivot right rightleft rightleft “move pivot out of the way”
Quicksort: Partition Algorithm 2 Smt Genap rightleft rightleft CROSSING: Quicksort recursively “move pivot back” Quicksort recursively
Quicksort: Implementation static 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
Quicksort: 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
Quicksort: Choosing The Pivot Ideal pivot: ◦ Median element Common pivot ◦ First element ◦ Element at the middle ◦ Median of three Smt Genap
Further Reading Chapter 8: Sorting Algorithm Smt Genap