Fundamentals of Algorithms MCS - 2 Lecture # 16. Quick Sort.

Slides:



Advertisements
Similar presentations
Chapter 9 continued: Quicksort
Advertisements

Introduction to Algorithms Quicksort
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
CSE 3101: Introduction to the Design and Analysis of Algorithms
§7 Quicksort -- the fastest known sorting algorithm in practice 1. The Algorithm void Quicksort ( ElementType A[ ], int N ) { if ( N < 2 ) return; pivot.
Nov 10, Fall 2009IAT 8001 Binary Search Sorting. Nov 10, Fall 2009IAT 8002 Search  Often want to search for an item in a list  In an unsorted list,
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
QuickSort 4 February QuickSort(S) Fast divide and conquer algorithm first discovered by C. A. R. Hoare in If the number of elements in.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
The Substitution method T(n) = 2T(n/2) + cn Guess:T(n) = O(n log n) Proof by Mathematical Induction: Prove that T(n)  d n log n for d>0 T(n)  2(d  n/2.
CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann.
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Quicksort COMP171 Fall Sorting II/ Slide 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N.
Introduction to Algorithms Chapter 7: Quick Sort.
1 Today’s Material Divide & Conquer (Recursive) Sorting Algorithms –QuickSort External Sorting.
CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
Sorting Algorithms and Average Case Time Complexity
Lecture 7COMPSCI.220.FS.T Algorithm MergeSort John von Neumann ( 1945 ! ): a recursive divide- and-conquer approach Three basic steps: –If the.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
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.
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.
CS 206 Introduction to Computer Science II 04 / 27 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 05 / 2008 Instructor: Michael Eckmann.
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.
Quicksort.
Quicksort.
Unit 061 Quick Sort csc326 Information Structures Spring 2009.
Quicksort
S: Application of quicksort on an array of ints: partitioning.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
CS 206 Introduction to Computer Science II 12 / 08 / 2008 Instructor: Michael Eckmann.
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Divide-And-Conquer Sorting Small instance.  n
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
CSC – 332 Data Structures Sorting
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
1 Sorting Algorithms Sections 7.1 to Comparison-Based Sorting Input – 2,3,1,15,11,23,1 Output – 1,1,2,3,11,15,23 Class ‘Animals’ – Sort Objects.
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Divide And Conquer A large instance is solved as follows:  Divide the large instance into smaller instances.  Solve the smaller instances somehow. 
1 Heapsort, Mergesort, and Quicksort Sections 7.5 to 7.7.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Sorting.
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.
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.
QuickSort. Yet another sorting algorithm! Usually faster than other algorithms on average, although worst-case is O(n 2 ) Divide-and-conquer: –Divide:
Intro. to Data Structures Chapter 7 Sorting Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Chapter 7 Sorting Sort is.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
Advanced Sorting.
Quicksort
Chapter 7 Sorting Spring 14
Advance Analysis of Algorithms
CSE 143 Lecture 23: quick sort.
Quicksort 1.
Quick Sort (11.2) CSE 2011 Winter November 2018.
slides adapted from Marty Stepp
EE 312 Software Design and Implementation I
Quicksort.
CSE 373 Data Structures and Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Quicksort.
Quicksort.
Presentation transcript:

Fundamentals of Algorithms MCS - 2 Lecture # 16

Quick Sort

 Fastest known sorting algorithm in practice.  It is being selected in most sorting libraries.  The quicksort algorithm sorts an unordered list based on the divide and conquer strategy.  It divides the unordered list into two sub-lists:  low elements sub-list, and  high elements sub-list, and then recursively sort these sub-lists.  Quicksort algorithm steps  Pick an element from the list, which is called a pivot.  Reorder the list with a rule that all elements which are less than the pivot come before the pivot, whereas all elements that are higher than the list come after the pivot. After partitioning the list, the pivot is in its position.  With the two sub-lists, apply the above steps recursively.

Quicksort  Divide step  Pick any element ( pivot ) piv in A  Partition A – {piv} into two disjoint groups A1 = {x  A – {piv} | x <= piv} A2 = {x  A – {piv} | x > piv}  Conquer step  Recursively sort A1 and A2  Combine step  The sorted A1 (by the time returned from recursion), followed by piv, followed by the sorted A2 (i.e., nothing extra needs to be done) piv A1A2 A

Example

Pseudo code of Quick Sort  Quick-Sort (array A, int left, int right)  1 if (left < right)  2 then piv ← a random index from [left ….. right]  3 swap a[piv] ↔ a[left]  4 q ← Partition (A, left, right) //q is the position of the pivot element  5 Quicksort (A, left, q-1)  6 Quicksort (A, q+1, right)

Partitioning  Partition algorithm partitions the array A[left … right] into three sub arrays about a pivot element piv.  A[left … q-1] whose elements are less than or equal to piv.  A[q] = piv,  A[q+1 … right] whose elements are greater than piv.

Partition Algorithm  PARTITION (array A, int left, int right)  1 piv ← A[left]  2 q ← left  3 for i ← left+1 to right  4 do if A[i] < piv  5 then q ← q+1  6 swap A[q] ↔ A[i]  7 swap A[left] ↔ A[q]  8 return q

Example

Analysis of Quick Sort  Assumptions:  A random pivot (no median-of-three partitioning)  No cutoff for small arrays  Running time  pivot selection: constant time, i.e. O(1)  partitioning: linear time, i.e. O(N)  running time of the two recursive calls  T(N)=T(i)+T(N-i-1)+cN where c is a constant  i: number of elements in S1  For very small arrays, quicksort does not perform as well as insertion sort  how small depends on many factors, such as the time spent making a recursive call, the compiler, etc  Do not use quicksort recursively for small arrays  Instead, use a sorting algorithm that is efficient for small arrays, such as insertion sort

Picking the Pivot  Use the first element as pivot  if the input is random, ok  if the input is presorted (or in reverse order)  all the elements go into S2 (or S1)  this happens consistently throughout the recursive calls  Results in O(n 2 ) behavior (Analyze this case later)  Choose the pivot randomly  generally safe  random number generation can be expensive  Use the median of the array  Partitioning always cuts the array into roughly half  An optimal quicksort (O(N log N))  However, hard to find the exact median  e.g., sort an array to pick the value in the middle

Good Luck ! ☻