CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7

Slides:



Advertisements
Similar presentations
Chapter 9 continued: Quicksort
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
§7 Quicksort -- the fastest known sorting algorithm in practice 1. The Algorithm void Quicksort ( ElementType A[ ], int N ) { if ( N < 2 ) return; pivot.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
QuickSort 4 February QuickSort(S) Fast divide and conquer algorithm first discovered by C. A. R. Hoare in If the number of elements in.
Spring 2015 Lecture 5: QuickSort & Selection
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
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.
Chapter 7: Sorting Algorithms
1 Today’s Material Divide & Conquer (Recursive) Sorting Algorithms –QuickSort External Sorting.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.
Lecture 7COMPSCI.220.FS.T Algorithm MergeSort John von Neumann ( 1945 ! ): a recursive divide- and-conquer approach Three basic steps: –If the.
Updated QuickSort Problem From a given set of n integers, find the missing integer from 0 to n using O(n) queries of type: “what is bit[j]
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
ALG0183 Algorithms & Data Structures Lecture 17 Quicksort 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks comparison sort worse-case.
Fundamentals of Algorithms MCS - 2 Lecture # 16. Quick Sort.
Quicksort CSC 172 SPRING 2002 LECTURE 13. Quicksort The basic quicksort algorithm is recursive Chosing the pivot Deciding how to partition Dealing with.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
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.
Sorting. Introduction Assumptions –Sorting an array of integers –Entire sort can be done in main memory Straightforward algorithms are O(N 2 ) More complex.
Quicksort.
Unit 061 Quick Sort csc326 Information Structures Spring 2009.
Quicksort
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
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).
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
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.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Sorting - 3 CS 202 – Fundamental Structures of Computer Science II.
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
DIVIDE & CONQUR ALGORITHMS Often written as first as a recursive algorithm Master’s Theorem: T(n) = aT(n/b) + cn i, for some constant integer i, and constants.
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.
Algorithm Analysis 2P03 © Dave Bockus Acknowledgments to Mark Allen Weiss 2014 Data Structures & Algorithm Analysis in Java.
CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing
Sorting Sorting includes internal sorting and external sorting – Internal sorting: the entire sort can be done in main memory, i.e., all elements needed.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
1 Chapter 7: Sorting (Insertion Sort, Shellsort) CE 221 Data Structures and Algorithms Izmir University of Economics Text: Read Weiss, § 7.1 – 7.4.
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.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
CS 146: Data Structures and Algorithms July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
1 Heapsort, Mergesort, and Quicksort Sections 7.5 to 7.7.
CSC 201 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing
Sorting Fundamental Data Structures and Algorithms Aleks Nanevski February 17, 2004.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Sorting.
Quicksort Quicksort is a well-known sorting algorithm that, in the worst case, it makes Θ(n 2 ) comparisons. Typically, quicksort is significantly faster.
Quicksort CSC 172. Quicksort The basic quicksort algorithm is recursive Chosing the pivot Deciding how to partition Dealing with duplicates Wrong decisions.
1 Chapter 7: Sorting (Insertion Sort, Shellsort) CE 221 Data Structures and Algorithms Izmir University of Economics Text: Read Weiss, § 7.1 – 7.4.
Intro. to Data Structures Chapter 7 Sorting Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Chapter 7 Sorting Sort is.
Chapter 5 Sorting There are several easy algorithms to sort in O(N 2 ), such as insertion sort. There is an algorithm, Shellsort, that is very simple to.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Chapter 7 Sorting Spring 14
Advance Analysis of Algorithms
Chapter 4: Divide and Conquer
Quick Sort (11.2) CSE 2011 Winter November 2018.
Unit-2 Divide and Conquer
EE 312 Software Design and Implementation I
Data Structures & Algorithms
Design and Analysis of Algorithms
CSE 332: Sorting II Spring 2016.
At the end of this session, Student will be able to:
Presentation transcript:

CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Sorting (Quicksort) CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Izmir University of Economics 1

Izmir University of Economics Quicksort As the name implies quicksort is the fastest known sorting algorithm in practice. Average running time is O(NlogN). Fast, due to a very tight and highly optimized inner loop. O(N2) worst-case performance, but this can be made exponentially unlikely with a little effort. Izmir University of Economics

Quicksort – Basic Algorithm Sorting an array S consists of 4 easy steps: quicksort(S) { 1. If the number of elements in S is 0 or 1, then return 2. Pick any element v in S. This is called the pivot. 3. Partition S-{v} into 2 disjoint groups: S1={xєS-{v}| x<=v} and S2={xєS-{v}| x>=v} 4. return {quicksort(S1) {v}, quicksort(S2)} } Partition step ambiguous for elements equal to the pivot. Izmir University of Economics

Izmir University of Economics Picking the Pivot Izmir University of Economics

Picking the Pivot Median-of-Three Partitioning The median of a group of N numbers is the floor(N/2)th largest number. Unfortunately, this is hard to calculate and would slow down the quicksort considerably. A good estimate can be obtained by picking three elements randomly and using the median of these as pivot. Common course is to use as pivot the median of the left, right and center elements. Example: input: 8, 1, 4, 9, 6, 3, 5, 2, 7, 0 left=8, right=0, center=6 (= floor((left+right)/2) ) Izmir University of Economics

Partitioning Strategy First step: get the pivot element out of the way by swapping it with the last element. Middle element is 6, hence, the pivot. i starts at the first element and j starts at the next-to-last element. 8 1 4 9 6 3 5 2 7 left pivot center right Izmir University of Economics

Partitioning Strategy – Example (1) move all the small elements to the left part of the array and all the large elements to the right part. Small and large are with respect to the pivot. While i is to the left of j, move i right skipping over elements that are smaller than the pivot. We move j left, skipping over elements that are larger than the pivot. When i and j have stopped, i is pointing at a large and j is pointing at a small element. If i is to the left of j, i and j are swapped. 8 1 4 9 3 5 2 7 6 i j 8 1 4 9 3 5 2 7 6 i j Izmir University of Economics

Partitioning Strategy – Example (2) 1 4 9 3 5 8 7 6 i j After first swap 2 1 4 9 3 5 8 7 6 i j 2 1 4 5 3 9 8 7 6 i j After second swap Izmir University of Economics

Partitioning Strategy – Example (3) 2 1 4 5 3 9 8 7 6 j i Before third swap At this stage, i and j have crossed, so no swap is performed. The final part is to swap the the pivot element with the element pointed to by i. 2 1 4 5 3 6 8 7 9 i pivot After swap with the pivot IMPORTANT: In case of equality both i and j will stop. Izmir University of Economics

Izmir University of Economics Small Arrays For very small arrays (N <= 20), quicksort does not perform well as insertion sort. Furthermore, because quicksort is recursive, these cases will occur frequently. A common solution is not to use quicksort recursively for small arrays, but instead use a sorting algorithm that is efficient for small arrays, such as insertion sort. Using this strategy can actually save about 15% in the running time. Izmir University of Economics

Quicksort – Implementation (1) typedef int ElementType; void Swap( ElementType *Lhs, ElementType *Rhs ) { ElementType Tmp = *Lhs; *Lhs = *Rhs; *Rhs = Tmp; } void Quicksort( ElementType A[ ], int N ) Qsort( A, 0, N - 1 ); Izmir University of Economics

Quicksort – Implementation (2) /* Return median of Left, Center, and Right */ /* Order these and hide the pivot */ ElementType Median3( ElementType A[ ], int Left, int Right ) { int Center = ( Left + Right ) / 2; if( A[ Left ] > A[ Center ] ) Swap( &A[ Left ], &A[ Center ] ); if( A[ Left ] > A[ Right ] ) Swap( &A[ Left ], &A[ Right ] ); if( A[ Center ] > A[ Right ] ) Swap( &A[ Center ], &A[ Right ] ); /* Invariant: A[ Left ] <= A[ Center ] <= A[ Right ] */ Swap( &A[ Center ], &A[ Right - 1 ] ); /* Hide pivot */ return A[ Right - 1 ]; /* Return pivot */ } Izmir University of Economics

Quicksort – Implementation (3) #define Cutoff ( 10 ) void Qsort( ElementType A[ ], int Left, int Right ) { int i, j; ElementType Pivot; if( Left + Cutoff <= Right ) { Pivot = Median3( A, Left, Right ); i = Left; j = Right - 1; for( ; ; ) { while( A[ ++i ] < Pivot ){ } while( A[ --j ] > Pivot ){ } if( i < j ) Swap( &A[ i ], &A[ j ] ); else break; } Swap( &A[ i ], &A[ Right - 1 ] ); /* Restore pivot */ Qsort( A, Left, i - 1 ); Qsort( A, i + 1, Right ); else /* Do an insertion sort on the subarray */ InsertionSort( A + Left, Right - Left + 1 ); Izmir University of Economics

Izmir University of Economics Quicksort – Be Careful This is not correct i = Left + 1; j = Right - 2; for( ; ; ) { while( A[ i ] < Pivot ) i++; while( A[ j ] > Pivot ) j--; if( i < j ) Swap( &A[ i ], &A[ j ] ); else break; } Izmir University of Economics

Izmir University of Economics Analysis of Quicksort Assume a random pivot (no median-of-three partitioning), no cutoff T(0) = T(1) = 1 T(N) = T(i) + T(N-i-1) + cN where i = |S1| is the number of elements in S1. Worst-case, Best-case, Average-Case analyses will be performed. Izmir University of Economics

Izmir University of Economics Worst-Case Analysis If the pivot is the smallest element, all the time. Then i = 0 and if we ignore T(0)=1, T(N)=T(N-1)+cN, N>1 T(N-1)=T(N-2)+c(N-1) T(N-2)=T(N-3)+c(N-2) ... T(2)=T(1)+c(2) adding up all these equations T(N)=T(1)+c = O(N2) Izmir University of Economics

Izmir University of Economics Best-Case Analysis In the best case the pivot is in the middle. T(N)=2T(N/2)+cN Divide both sides of the equation by N T(N)/N=T(N/2)/(N/2)+c T(N/2)/(N/2)=T(N/4)/(N/4)+c ... T(2)/(2)=T(1)/(1)+c adding up all the equations T(N)/N= T(1)/1+clogN T(N)=cNlogN+N=O(NlogN) Izmir University of Economics

Average-Case Analysis Assume each of the sizes for S1is equally likely and hence probability is 1/N. Subtract (2) from (1) Divide both sides by N(N+1) Now we can telescope adding all equations Izmir University of Economics