Presentation is loading. Please wait.

Presentation is loading. Please wait.

Concepts of Algorithms CSC-244 Unit 17 & 18 Divide-and-conquer Algorithms Quick Sort Shahid Iqbal Lone Computer College Qassim University K.S.A.

Similar presentations


Presentation on theme: "Concepts of Algorithms CSC-244 Unit 17 & 18 Divide-and-conquer Algorithms Quick Sort Shahid Iqbal Lone Computer College Qassim University K.S.A."— Presentation transcript:

1 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 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.

3 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

4 Example We are given array of n integers to sort: 402010806050730100 4 CSC 244 Concepts of Algorithms

5 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: 402010806050730100 5 CSC 244 Concepts of Algorithms

6 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

7 402010806050730100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 7 CSC 244 Concepts of Algorithms

8 402010806050730100 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

9 402010806050730100 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

10 402010806050730100 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

11 402010806050730100 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

12 402010806050730100 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

13 402010806050730100 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

14 402010306050780100 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

15 402010306050780100 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 1. 15 CSC 244 Concepts of Algorithms

16 402010306050780100 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 1. 16 CSC 244 Concepts of Algorithms

17 402010306050780100 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 1. 17 CSC 244 Concepts of Algorithms

18 402010306050780100 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 1. 18 CSC 244 Concepts of Algorithms

19 402010306050780100 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 1. 19 CSC 244 Concepts of Algorithms

20 402010306050780100 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 1. 20 CSC 244 Concepts of Algorithms

21 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. 402010307506080100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 21 CSC 244 Concepts of Algorithms

22 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. 402010307506080100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 22 CSC 244 Concepts of Algorithms

23 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. 402010307506080100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 23 CSC 244 Concepts of Algorithms

24 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. 402010307506080100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 24 CSC 244 Concepts of Algorithms

25 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. 402010307506080100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 25 CSC 244 Concepts of Algorithms

26 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. 402010307506080100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 26 CSC 244 Concepts of Algorithms

27 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. 402010307506080100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 27 CSC 244 Concepts of Algorithms

28 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. 402010307506080100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 28 CSC 244 Concepts of Algorithms

29 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. 402010307506080100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 29 CSC 244 Concepts of Algorithms

30 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] 402010307506080100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 30 CSC 244 Concepts of Algorithms

31 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] 720103040506080100 pivot_index = 4 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 31 CSC 244 Concepts of Algorithms

32 Partition Result 720103040506080100 [0] [1] [2] [3] [4] [5] [6] [7] [8] <= data[pivot]> data[pivot] 32 CSC 244 Concepts of Algorithms

33 Recursion: Quicksort Sub-arrays 720103040506080100 [0] [1] [2] [3] [4] [5] [6] [7] [8] <= data[pivot]> data[pivot] 33 CSC 244 Concepts of Algorithms

34 Quicksort Analysis Assume that keys are random, uniformly distributed. What is best case running time? 34 CSC 244 Concepts of Algorithms

35 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

36 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

37 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

38 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

39 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

40 Quicksort Analysis Assume that keys are random, uniformly distributed. Best case running time: O(n log 2 n) 40 CSC 244 Concepts of Algorithms

41 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

42 Quicksort: Worst Case Assume first element is chosen as pivot. Assume we get array that is already in order: 24101213505763100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 42 CSC 244 Concepts of Algorithms

43 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] 24101213505763100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 43 CSC 244 Concepts of Algorithms

44 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] 24101213505763100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 44 CSC 244 Concepts of Algorithms

45 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] 24101213505763100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 45 CSC 244 Concepts of Algorithms

46 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] 24101213505763100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 46 CSC 244 Concepts of Algorithms

47 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] 24101213505763100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 47 CSC 244 Concepts of Algorithms

48 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] 24101213505763100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] I J 48 CSC 244 Concepts of Algorithms

49 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] 24101213505763100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] > data[pivot]<= data[pivot] 49 CSC 244 Concepts of Algorithms

50 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

51 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

52 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

53 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

54 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

55 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

56 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

57 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); }

58 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

59 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

60 END 60 CSC 244 Concepts of Algorithms


Download ppt "Concepts of Algorithms CSC-244 Unit 17 & 18 Divide-and-conquer Algorithms Quick Sort Shahid Iqbal Lone Computer College Qassim University K.S.A."

Similar presentations


Ads by Google