Quicksort Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.

Slides:



Advertisements
Similar presentations
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
Advertisements

Introduction to Algorithms Quicksort
David Luebke 1 4/22/2015 CS 332: Algorithms Quicksort.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
CSE 3101: Introduction to the Design and Analysis of Algorithms
ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions.
Lecture 2: Divide and Conquer algorithms Phan Thị Hà Dương
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
Spring 2015 Lecture 5: QuickSort & Selection
Analysis of Algorithms CS 477/677 Randomizing Quicksort Instructor: George Bebis (Appendix C.2, Appendix C.3) (Chapter 5, Chapter 7)
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.
Quicksort Ack: Several slides from Prof. Jim Anderson’s COMP 750 notes. UNC Chapel Hill1.
Introduction to Algorithms Chapter 7: Quick Sort.
7.Quicksort Hsu, Lih-Hsing. Computer Theory Lab. Chapter 7P Description of quicksort Divide Conquer Combine.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Ch. 7 - QuickSort Quick but not Guaranteed. Ch.7 - QuickSort Another Divide-and-Conquer sorting algorithm… As it turns out, MERGESORT and HEAPSORT, although.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 4 Comparison-based sorting Why sorting? Formal analysis of Quick-Sort Comparison.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
CS421 - Course Information Website Syllabus Schedule The Book:
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Quicksort CIS 606 Spring Quicksort Worst-case running time: Θ(n 2 ). Expected running time: Θ(n lg n). Constants hidden in Θ(n lg n) are small.
Qsort - 1 Lin / Devi Comp 122 d(n)d(n) Today’s puzzle  How far can you reach with a stack of n blocks, each 2 units long?
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place.
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
10 Algorithms in 20th Century Science, Vol. 287, No. 5454, p. 799, February 2000 Computing in Science & Engineering, January/February : The Metropolis.
Efficient Algorithms Quicksort. Quicksort A common algorithm for sorting non-sorted arrays. Worst-case running time is O(n 2 ), which is actually the.
Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Introduction to Algorithms Jiafen Liu Sept
David Luebke 1 6/3/2016 CS 332: Algorithms Analyzing Quicksort: Average Case.
CS 2133: Data Structures Quicksort. Review: Heaps l A heap is a “complete” binary tree, usually represented as an array:
Deterministic and Randomized Quicksort Andreas Klappenecker.
QuickSort (Ch. 7) Like Merge-Sort, based on the three-step process of divide- and-conquer. Input: An array A[1…n] of comparable elements, the starting.
Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 2: Getting Started.
COSC 3101A - Design and Analysis of Algorithms 4 Quicksort Medians and Order Statistics Many of these slides are taken from Monica Nicolescu, Univ. of.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
David Luebke 1 2/19/2016 Priority Queues Quicksort.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 7: Sorting and Directed Graphs.
QuickSort. Yet another sorting algorithm! Usually faster than other algorithms on average, although worst-case is O(n 2 ) Divide-and-conquer: –Divide:
Mudasser Naseer 1 3/4/2016 CSC 201: Design and Analysis of Algorithms Lecture # 6 Bubblesort Quicksort.
Sorting. 2 The Sorting Problem Input: A sequence of n numbers a 1, a 2,..., a n Output: A permutation (reordering) a 1 ’, a 2 ’,..., a n ’ of the input.
CSC317 1 Hiring problem-review Cost to interview (low C i ) Cost to fire/hire … (expensive C h ) n number of candidates m hired O (c i n + c h m) Independent.
1 Chapter 7 Quicksort. 2 About this lecture Introduce Quicksort Running time of Quicksort – Worst-Case – Average-Case.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Analysis of Algorithms CS 477/677
Quick Sort Divide: Partition the array into two sub-arrays
Order Statistics Comp 122, Spring 2004.
Introduction to Algorithms Prof. Charles E. Leiserson
Advance Analysis of Algorithms
CSC 413/513: Intro to Algorithms
CO 303 Algorithm Analysis And Design Quicksort
Ch 7: Quicksort Ming-Te Chi
Lecture 3 / 4 Algorithm Analysis
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Order Statistics Comp 550, Spring 2015.
CS 583 Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 332: Algorithms Quicksort David Luebke /9/2019.
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
Chapter 7 Quicksort.
Order Statistics Comp 122, Spring 2004.
CS200: Algorithm Analysis
Quicksort and Randomized Algs
Quicksort Quick sort Correctness of partition - loop invariant
Presentation transcript:

Quicksort Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill

Performance  A triumph of analysis by C.A.R. Hoare  Worst-case execution time –  (n 2 ).  Average-case execution time –  (n lg n). »How do the above compare with the complexities of other sorting algorithms?  Empirical and analytical studies show that quicksort can be expected to be twice as fast as its competitors.

Design  Follows the divide-and-conquer paradigm.  Divide: Partition (separate) the array A[p..r] into two (possibly empty) subarrays A[p..q–1] and A[q+1..r]. »Each element in A[p..q–1]  A[q]. »A[q]  each element in A[q+1..r]. »Index q is computed as part of the partitioning procedure.  Conquer: Sort the two subarrays by recursive calls to quicksort.  Combine: The subarrays are sorted in place – no work is needed to combine them.  How do the divide and combine steps of quicksort compare with those of merge sort?

Pseudocode Quicksort(A, p, r) if p < r then q := Partition(A, p, r); Quicksort(A, p, q – 1); Quicksort(A, q + 1, r) fi Quicksort(A, p, r) if p < r then q := Partition(A, p, r); Quicksort(A, p, q – 1); Quicksort(A, q + 1, r) fi Partition(A, p, r) x, i := A[r], p – 1; for j := p to r – 1 do if A[j]  x then i := i + 1; A[i]  A[j] fi od; A[i + 1]  A[r]; return i + 1 Partition(A, p, r) x, i := A[r], p – 1; for j := p to r – 1 do if A[j]  x then i := i + 1; A[i]  A[j] fi od; A[i + 1]  A[r]; return i A[p..r] A[p..q – 1] A[q+1..r]  5  5 Partition 5

Example p r initially: note: pivot (x) = 6 i j next iteration: i j next iteration: i j next iteration: i j next iteration: i j Partition(A, p, r) x, i := A[r], p – 1; for j := p to r – 1 do if A[j]  x then i := i + 1; A[i]  A[j] fi od; A[i + 1]  A[r]; return i + 1 Partition(A, p, r) x, i := A[r], p – 1; for j := p to r – 1 do if A[j]  x then i := i + 1; A[i]  A[j] fi od; A[i + 1]  A[r]; return i + 1

Example (Continued) next iteration: i j next iteration: i j next iteration: i j next iteration: i j next iteration: i j next iteration: i j after final swap: i j Partition(A, p, r) x, i := A[r], p – 1; for j := p to r – 1 do if A[j]  x then i := i + 1; A[i]  A[j] fi od; A[i + 1]  A[r]; return i + 1 Partition(A, p, r) x, i := A[r], p – 1; for j := p to r – 1 do if A[j]  x then i := i + 1; A[i]  A[j] fi od; A[i + 1]  A[r]; return i + 1

Partitioning  Select the last element A[r] in the subarray A[p..r] as the pivot – the element around which to partition.  As the procedure executes, the array is partitioned into four (possibly empty) regions. 1.A[p..i] — All entries in this region are  pivot. 2.A[i+1..j – 1] — All entries in this region are > pivot. 3.A[r] = pivot. 4.A[j..r – 1] — Not known how they compare to pivot.  The above hold before each iteration of the for loop, and constitute a loop invariant. (4 is not part of the LI.)

Correctness of Partition  Use loop invariant.  Initialization: »Before first iteration A[p..i] and A[i+1..j – 1] are empty – Conds. 1 and 2 are satisfied (trivially). r is the index of the pivot – Cond. 3 is satisfied.  Maintenance: »Case 1: A[j] > x Increment j only. LI is maintained. Partition(A, p, r) x, i := A[r], p – 1; for j := p to r – 1 do if A[j]  x then i := i + 1; A[i]  A[j] fi od; A[i + 1]  A[r]; return i + 1 Partition(A, p, r) x, i := A[r], p – 1; for j := p to r – 1 do if A[j]  x then i := i + 1; A[i]  A[j] fi od; A[i + 1]  A[r]; return i + 1

Correctness of Partition >x>x x p i j r  x x > x x p i j r  x x Case 1:

Correctness of Partition  x x x p i j r  x x > x  Case 2: A[j]  x »Increment i »Swap A[i] and A[j] Condition 1 is maintained. »Increment j Condition 2 is maintained. »A[r] is unaltered. Condition 3 is maintained.  x x > x x p i j r

Correctness of Partition  Termination: »When the loop terminates, j = r, so all elements in A are partitioned into one of the three cases: A[p..i]  pivot A[i+1..j – 1] > pivot A[r] = pivot  The last two lines swap A[i+1] and A[r]. »Pivot moves from the end of the array to between the two subarrays. »Thus, procedure partition correctly performs the divide step.

Complexity of Partition  PartitionTime(n) is given by the number of iterations in the for loop.   (n) : n = r – p + 1. Partition(A, p, r) x, i := A[r], p – 1; for j := p to r – 1 do if A[j]  x then i := i + 1; A[i]  A[j] fi od; A[i + 1]  A[r]; return i + 1 Partition(A, p, r) x, i := A[r], p – 1; for j := p to r – 1 do if A[j]  x then i := i + 1; A[i]  A[j] fi od; A[i + 1]  A[r]; return i + 1

Algorithm Performance Running time of quicksort depends on whether the partitioning is balanced or not.  Worst-Case Partitioning (Unbalanced Partitions): »Occurs when every call to partition results in the most unbalanced partition. »Partition is most unbalanced when Subproblem 1 is of size n – 1, and subproblem 2 is of size 0 or vice versa. pivot  every element in A[p..r – 1] or pivot < every element in A[p..r – 1]. »Every call to partition is most unbalanced when Array A[1..n] is sorted or reverse sorted!

Worst-case Partition Analysis Running time for worst-case partitions at each recursive level: T(n) = T(n – 1) + T(0) + PartitionTime(n) = T(n – 1) +  (n) =  k=1 to n  (k) =  (  k=1 to n k ) =  (n 2 ) n n – 1 n – 2 n – n Recursion tree for worst-case partition

Best-case Partitioning  Size of each subproblem  n/2. »One of the subproblems is of size  n/2  »The other is of size  n/2   1.  Recurrence for running time »T(n)  2T(n/2) + PartitionTime(n) = 2T(n/2) +  (n)  T(n) =  (n lg n)

Recursion Tree for Best-case Partition cn cn/2 cn/4 cccccc lg n cn Total : O(n lg n) cn

Variations  Quicksort is not very efficient on small lists.  This is a problem because Quicksort will be called on lots of small lists.  Fix 1: Use Insertion Sort on small problems.  Fix 2: Leave small problems unsorted. Fix with one final Insertion Sort at end. »Note: Insertion Sort is very fast on almost-sorted lists.

Unbalanced Partition Analysis What happens if we get poorly-balanced partitions, e.g., something like: T(n)  T(9n/10) + T(n/10) +  (n)? Still get  (n lg n)!! (As long as the split is of constant proportionality.) Intuition: Can divide n by c > 1 only  (lg n) times before getting 1. n  n/c  n/c 2    1= n/c log c n Roughly log c n levels; Cost per level is O(n). (Remember: Different base logs are related by a constant.)  n n  n n  n n

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Intuition for the Average Case  Partitioning is unlikely to happen in the same way at every level. »Split ratio is different for different levels. (Contrary to our assumption in the previous slide.)  Partition produces a mix of “good” and “bad” splits, distributed randomly in the recursion tree.  What is the running time likely to be in such a case?

Intuition for the Average Case n 0 n – 1 (n – 1)/2 – 1 (n – 1)/2 (n)(n) Bad split followed by a good split: Produces subarrays of sizes 0, (n – 1)/2 – 1, and (n – 1)/2. Cost of partitioning :  (n) +  (n-1) =  (n). n (n – 1)/2 (n)(n) Good split at the first level: Produces two subarrays of size (n – 1)/2. Cost of partitioning :  (n). Situation at the end of case 1 is not worse than that at the end of case 2. When splits alternate between good and bad, the cost of bad split can be absorbed into the cost of good split. Thus, running time is O(n lg n), though with larger hidden constants.

Randomized Quicksort  Want to make running time independent of input ordering.  How can we do that? »Make the algorithm randomized. »Make every possible input equally likely. Can randomly shuffle to permute the entire array. For quicksort, it is sufficient if we can ensure that every element is equally likely to be the pivot. So, we choose an element in A[p..r] and exchange it with A[r]. Because the pivot is randomly chosen, we expect the partitioning to be well balanced on average.

Randomized Version Randomized-Partition(A, p, r) i := Random(p, r); A[r]  A[i]; Partition(A, p, r) Randomized-Partition(A, p, r) i := Random(p, r); A[r]  A[i]; Partition(A, p, r) Randomized-Quicksort(A, p, r) if p < r then q := Randomized-Partition(A, p, r); Randomized-Quicksort(A, p, q – 1); Randomized-Quicksort(A, q + 1, r) fi Randomized-Quicksort(A, p, r) if p < r then q := Randomized-Partition(A, p, r); Randomized-Quicksort(A, p, q – 1); Randomized-Quicksort(A, q + 1, r) fi Want to make running time independent of input ordering.