Concepts of Algorithms CSC-244 Unit 17 & 18 Divide-and-conquer Algorithms Quick Sort Shahid Iqbal Lone Computer College Qassim University K.S.A.
2 Divide and Conquer CSC 244 Concepts of Algorithms The most well known algorithm design strategy: 1. Divide a large problem into two or more smaller problems 2.Solve smaller problems recursively 3.Obtain solution to original (larger) problem by combining/merging these solutions of small problems.
Quicksort Algorithm Given an array of n elements (e.g., integers): If array only contains one element, return Else –pick one element to use as pivot. –Partition elements into two sub-arrays: Elements less than or equal to pivot Elements greater than pivot –Quicksort two sub-arrays –Return results 3 CSC 244 Concepts of Algorithms
Example We are given array of n integers to sort: CSC 244 Concepts of Algorithms
Pick Pivot Element There are a number of ways to pick the pivot element. In this example, we will use the first element in the array: CSC 244 Concepts of Algorithms
Partitioning Array Given a pivot, partition the elements of the array such that the resulting array consists of: 1.One sub-array that contains elements <= pivot 2.Another sub-array that contains elements > pivot The sub-arrays are stored in the original data array. Partitioning loops through, swapping elements below/above pivot. 6 CSC 244 Concepts of Algorithms
pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 7 CSC 244 Concepts of Algorithms
pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 1.While data[I] <= data[pivot] ++I 8 CSC 244 Concepts of Algorithms
pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 1.While data[I] <= data[pivot] ++I 9 CSC 244 Concepts of Algorithms
pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 1.While data[I] <= data[pivot] ++I 10 CSC 244 Concepts of Algorithms
pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 11 CSC 244 Concepts of Algorithms
pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 12 CSC 244 Concepts of Algorithms
pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 13 CSC 244 Concepts of Algorithms
pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 14 CSC 244 Concepts of Algorithms
pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to CSC 244 Concepts of Algorithms
pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to CSC 244 Concepts of Algorithms
pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to CSC 244 Concepts of Algorithms
pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to CSC 244 Concepts of Algorithms
pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to CSC 244 Concepts of Algorithms
pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to CSC 244 Concepts of Algorithms
1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 21 CSC 244 Concepts of Algorithms
1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 22 CSC 244 Concepts of Algorithms
1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 23 CSC 244 Concepts of Algorithms
1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 24 CSC 244 Concepts of Algorithms
1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 25 CSC 244 Concepts of Algorithms
1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 26 CSC 244 Concepts of Algorithms
1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 27 CSC 244 Concepts of Algorithms
1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 28 CSC 244 Concepts of Algorithms
1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 29 CSC 244 Concepts of Algorithms
1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to 1. 5.Swap data[J] and data[pivot] pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 30 CSC 244 Concepts of Algorithms
1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to 1. 5.Swap data[J] and data[pivot_index] pivot_index = 4 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 31 CSC 244 Concepts of Algorithms
Partition Result [0] [1] [2] [3] [4] [5] [6] [7] [8] <= data[pivot]> data[pivot] 32 CSC 244 Concepts of Algorithms
Recursion: Quicksort Sub-arrays [0] [1] [2] [3] [4] [5] [6] [7] [8] <= data[pivot]> data[pivot] 33 CSC 244 Concepts of Algorithms
Quicksort Analysis Assume that keys are random, uniformly distributed. What is best case running time? 34 CSC 244 Concepts of Algorithms
Quicksort Analysis Assume that keys are random, uniformly distributed. What is best case running time? –Recursion: 1.Partition splits array in two sub-arrays of size n/2 2.Quicksort each sub-array 35 CSC 244 Concepts of Algorithms
Quicksort Analysis Assume that keys are random, uniformly distributed. What is best case running time? –Recursion: 1.Partition splits array in two sub-arrays of size n/2 2.Quicksort each sub-array –Depth of recursion tree? 36 CSC 244 Concepts of Algorithms
Quicksort Analysis Assume that keys are random, uniformly distributed. What is best case running time? –Recursion: 1.Partition splits array in two sub-arrays of size n/2 2.Quicksort each sub-array –Depth of recursion tree? O(log 2 n) 37 CSC 244 Concepts of Algorithms
Quicksort Analysis Assume that keys are random, uniformly distributed. What is best case running time? –Recursion: 1.Partition splits array in two sub-arrays of size n/2 2.Quicksort each sub-array –Depth of recursion tree? O(log 2 n) –Number of accesses in partition? 38 CSC 244 Concepts of Algorithms
Quicksort Analysis Assume that keys are random, uniformly distributed. What is best case running time? –Recursion: 1.Partition splits array in two sub-arrays of size n/2 2.Quicksort each sub-array –Depth of recursion tree? O(log 2 n) –Number of accesses in partition? O(n) 39 CSC 244 Concepts of Algorithms
Quicksort Analysis Assume that keys are random, uniformly distributed. Best case running time: O(n log 2 n) 40 CSC 244 Concepts of Algorithms
Quicksort Analysis Assume that keys are random, uniformly distributed. Best case running time: O(n log 2 n) Worst case running time? 41 CSC 244 Concepts of Algorithms
Quicksort: Worst Case Assume first element is chosen as pivot. Assume we get array that is already in order: pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 42 CSC 244 Concepts of Algorithms
1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to 1. 5.Swap data[J] and data[pivot] pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 43 CSC 244 Concepts of Algorithms
1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to 1. 5.Swap data[J] and data[pivot] pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 44 CSC 244 Concepts of Algorithms
1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to 1. 5.Swap data[J] and data[pivot] pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 45 CSC 244 Concepts of Algorithms
1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to 1. 5.Swap data[J] and data[pivot] pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 46 CSC 244 Concepts of Algorithms
1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to 1. 5.Swap data[J] and data[pivot] pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 47 CSC 244 Concepts of Algorithms
1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to 1. 5.Swap data[J] and data[pivot] pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 48 CSC 244 Concepts of Algorithms
1.While data[I] <= data[pivot] ++I 2.While data[J] > data[pivot] --J 3.If I < J swap data[I] and data[J] 4.While J > I, go to 1. 5.Swap data[J] and data[pivot] pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] > data[pivot]<= data[pivot] 49 CSC 244 Concepts of Algorithms
Quicksort Analysis Assume that keys are random, uniformly distributed. Best case running time: O(n log 2 n) Worst case running time? –Recursion: 1.Partition splits array in two sub-arrays: one sub-array of size 0 the other sub-array of size n-1 2.Quicksort each sub-array –Depth of recursion tree? 50 CSC 244 Concepts of Algorithms
Quicksort Analysis Assume that keys are random, uniformly distributed. Best case running time: O(n log 2 n) Worst case running time? –Recursion: 1.Partition splits array in two sub-arrays: one sub-array of size 0 the other sub-array of size n-1 2.Quicksort each sub-array –Depth of recursion tree? O(n) 51 CSC 244 Concepts of Algorithms
Quicksort Analysis Assume that keys are random, uniformly distributed. Best case running time: O(n log 2 n) Worst case running time? –Recursion: 1.Partition splits array in two sub-arrays: one sub-array of size 0 the other sub-array of size n-1 2.Quicksort each sub-array –Depth of recursion tree? O(n) –Number of accesses per partition? 52 CSC 244 Concepts of Algorithms
Quicksort Analysis Assume that keys are random, uniformly distributed. Best case running time: O(n log 2 n) Worst case running time? –Recursion: 1.Partition splits array in two sub-arrays: one sub-array of size 0 the other sub-array of size n-1 2.Quicksort each sub-array –Depth of recursion tree? O(n) –Number of accesses per partition? O(n) 53 CSC 244 Concepts of Algorithms
Quicksort Analysis Assume that keys are random, uniformly distributed. Best case running time: O(n log 2 n) Worst case running time: O(n 2 )!!! 54 CSC 244 Concepts of Algorithms
Quicksort Analysis Assume that keys are random, uniformly distributed. Best case running time: O(n log 2 n) Worst case running time: O(n 2 )!!! What can we do to avoid worst case? 55 CSC 244 Concepts of Algorithms
Improved Pivot Selection Pick median value of three elements from data array: data[0], data[n/2], and data[n-1]. Use this median value as pivot. 56 CSC 244 Concepts of Algorithms
C-Language Code for Quick Sort #include void get_Values( int data[], int N) { cout<<"\n\t\t Put "<<N<<" random values..\n\n"; for(int i=0;i<N;i++) {cin>>data[i];} } void display( int data[], int N) { for(int i=0;i<N;i++) {cout<<data[i]<<"\t";} } 57CSC 244 Concepts of Algorithms void Qsort(int data[], int left, int right) { int pivot, i, j; i = left; j = right; pivot = (left + right) / 2; do { while (data[i] < data[pivot]) i++; while (data[j] > data[pivot] ) j--; if (i <= j) { int temp = data[i]; data[i] = data[j]; data[j] = temp; i++; j--; } }while (i <= j); if (left < j) Qsort(data, left, j); if (i < right) Qsort(data, i, right); }
C-Language Code for Quick Sort int main() { int A[100]; int N; cout "; cin>>N; get_Values(A, N); cout<<"Before sort:\n\n"; display(A, N); Qsort(A,0,N-1); cout<<"\n\nAfter sort:\n\n"; display(A, N); return 0; } // end of main ( ) function 58CSC 244 Concepts of Algorithms
QUICK SORT C-Code 59 CSC 244 Concepts of Algorithms #include void quicksort(int [10],int,int); int main( ) { int x[20],size,i; printf("Enter size of the array: "); scanf("%d",&size); printf("Enter %d elements: ",size); for(i=0;i<size;i++) scanf("%d",&x[i]); quicksort(x,0,size-1); printf("Sorted elements: "); for(i=0;i<size;i++) printf(" %d",x[i]); return 0; } void quicksort(int x[10],int first,int last) { int pivot,j,temp,i; if(first<last) { pivot=first; i=first; j=last; while(i<j) { while(x[i]<=x[pivot]&&i<last) i++; while(x[j]>x[pivot]) j--; if(i<j) { temp=x[i]; x[i]=x[j]; x[j]=temp; } } // closing of while temp=x[pivot]; x[pivot]=x[j]; x[j]=temp; quicksort(x,first,j-1); quicksort(x,j+1,last); } // closing of if }// closing of function
END 60 CSC 244 Concepts of Algorithms