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

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Quicksort File: D|\data\bit143\Fall01\day1212\quicksort.sdd BIT Gerard Harrison Divide and Conquer Reduce the problem by reducing the data set. The.
21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 1 Class 15 - Recursive sorting methods r Processing arrays by recursion r Divide-and-conquer.
Quicksort Quicksort     29  9.
Analysis of Quicksort. Quicksort Algorithm Given an array of n elements (e.g., integers): If array only contains one element, return Else –pick one element.
CSC2100B Quick Sort and Merge Sort Xin 1. Quick Sort Efficient sorting algorithm Example of Divide and Conquer algorithm Two phases ◦ Partition phase.
Introduction to Algorithms Chapter 7: Quick Sort.
QuickSort The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Sorting Algorithms and Average Case Time Complexity
© 2004 Goodrich, Tamassia QuickSort1 Quick-Sort     29  9.
CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
CS Data Structures I Chapter 10 Algorithm Efficiency & Sorting III.
Quicksort Divide-and-Conquer. Quicksort Algorithm Given an array S of n elements (e.g., integers): If array only contains one element, return it. Else.
Sorting Chapter 9.
Fundamentals of Algorithms MCS - 2 Lecture # 16. Quick Sort.
CS 162 Intro to Programming II Quick Sort 1. Quicksort Maybe the most commonly used algorithm Quicksort is also a divide and conquer algorithm Advantage.
1 Issues with Matrix and Vector Issues with Matrix and Vector Quicksort Quicksort Determining Algorithm Efficiency Determining Algorithm Efficiency Substitution.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
Faster Sorting Methods Chapter 9. 2 Chapter Contents Merge Sort Merging Arrays Recursive Merge Sort The Efficiency of Merge Sort Merge Sort in the Java.
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 10/23/2009.
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. Sorting III 1 An Introduction to Sorting.
Quicksort. 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N 2 ) n But, the worst case seldom.
CS2420: Lecture 10 Vladimir Kulyukin Computer Science Department Utah State University.
Unit 281 Merge- and Quick Sort Merge Sort Quick Sort Exercises.
Quicksort.
Quicksort.
Unit 061 Quick Sort csc326 Information Structures Spring 2009.
© 2004 Goodrich, Tamassia Merge Sort1 Quick-Sort     29  9.
Quicksort
Unit 281 Merge- and Quick Sort Merge Sort Quick Sort Exercises.
1 7.5 Heapsort Average number of comparison used to heapsort a random permutation of N items is 2N logN - O (N log log N).
Mergesort and Quicksort Chapter 8 Kruse and Ryba.
CS 206 Introduction to Computer Science II 12 / 08 / 2008 Instructor: Michael Eckmann.
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
UNIT-4. Searching Methods:  Linear Search  Binary Search Sorting Techniques:  Bubble Sort  Selection Sort  Insertion Sort  Quick Sort  Merge Sort.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
Recursive Quicksort Data Structures in Java with JUnit ©Rick Mercer.
Sorting. Introduction Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
Sorting part 2 Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2.Solve smaller instances.
Sorting: Advanced Techniques Smt Genap
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
Quicksort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer.
Divide and Conquer Strategy
Chapter 9 Sorting 1. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step.
Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University.
Sorting divide and conquer. Divide-and-conquer  a recursive design technique  solve small problem directly  divide large problem into two subproblems,
Review 1 Insertion Sort Insertion Sort Algorithm Time Complexity Best case Average case Worst case Examples.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Nothing is particularly hard if you divide it into small jobs. Henry Ford Nothing is particularly hard if you divide it into small jobs. Henry Ford.
Sorting Quick, Merge & Radix Divide-and-conquer Technique subproblem 2 of size n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to.
Quicksort This is probably the most popular sorting algorithm. It was invented by the English Scientist C.A.R. Hoare It is popular because it works well.
Sorting part 2 Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
QuickSort by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data.
Computer Sciences Department1. Sorting algorithm 4 Computer Sciences Department3.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Quick-Sort To understand quick-sort, let’s look at a high-level description of the algorithm 1) Divide : If the sequence S has 2 or more elements, select.
Quicksort "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Quicksort Algorithm Given an array of n elements (e.g., integers):

Data Structures and Algorithms CS 244
Presentation transcript:

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