Download presentation
Presentation is loading. Please wait.
1
Sorting: Advanced Techniques Smt Genap 2011-2012
2
Outline Divide-and-conquer sorting algorithms: ◦ Mergesort ◦ Quicksort For each algorithm: ◦ Idea ◦ Example ◦ Implementation ◦ Running time for each algorithm Smt Genap 2011-2012
3
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 2011-2012 12340436503442581023344042435865 Counter A Counter B Counter c
4
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 2011-2012 Assume A and B are sorted and |C| = |A| + |B|
5
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 2011-2012
6
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 2011-2012
7
Mergesort: Example Smt Genap 2011-2012 4021433650583424402143365058342440214336505834244021433650583424 split
8
Mergesort: Example Smt Genap 2011-2012 402143365058342421365584244012433650583442 split merge 1240343650583442
9
Mergesort: Example Smt Genap 2011-2012 merge 124034365058344212340436503442580123344042435862
10
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 2011-2012 How to merge the two subarrays of A without any “temporary” space?
11
Mergesort: Merge Implementation Implement operation to merge two sorted subarrays: public static void merge(int[] A, int l, int c, int r) { } Smt Genap 2011-2012
12
Mergesort: Analysis Running Time: O(n log n) Why? Smt Genap 2011-2012
13
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 2011-2012
14
Quicksort: Select Pivot Smt Genap 2011-2012 40 2 1 3 43 65 0 58 3 42 4
15
Quicksort: Partition Smt Genap 2011-2012 2 1 3 43 65 0 58 3 42 4 40
16
Quicksort: Recursive Sort & Merge Smt Genap 2011-2012 213 4365 0 58 3 42 4 40 213 4365 0 58 3 42 4 40
17
Quicksort: Partition Algorithm 1 Smt Genap 2011-2012 402143365058342440214336505834244021433650583424 left 40 right 3214336505840424
18
Quicksort: Partition Algorithm 1 Smt Genap 2011-2012 402143365058342440214336505834244021433650583424 leftright
19
Quicksort: Partition Algorithm 1 Smt Genap 2011-2012 3210344042435865213342213034342652130344265584340214336505834244021433650583424332323
20
Quicksort: Partition Algorithm 2 Smt Genap 2011-2012 402143365058342440 original pivot = 40 while < pivot left++ while >= pivot right-- 4214336505834240 21433650583424 rightleft 4021336505843424 rightleft “move pivot out of the way”
21
Quicksort: Partition Algorithm 2 Smt Genap 2011-2012 4021336505843424 rightleft 40213306558434246521330405843424 rightleft CROSSING: Quicksort recursively “move pivot back” Quicksort recursively
22
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 2011-2012
23
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 2011-2012
24
Quicksort: Choosing The Pivot Ideal pivot: ◦ Median element Common pivot ◦ First element ◦ Element at the middle ◦ Median of three Smt Genap 2011-2012
25
Further Reading Chapter 8: Sorting Algorithm Smt Genap 2011-2012
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.