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.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

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.
ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions.
Using Divide and Conquer for Sorting
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 Instructor: Monica Nicolescu Lecture 5.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
Spring 2015 Lecture 5: QuickSort & Selection
David Luebke 1 5/20/2015 CS 332: Algorithms Quicksort.
Analysis of Algorithms CS 477/677 Randomizing Quicksort Instructor: George Bebis (Appendix C.2, Appendix C.3) (Chapter 5, Chapter 7)
Quicksort Ack: Several slides from Prof. Jim Anderson’s COMP 750 notes. UNC Chapel Hill1.
Introduction to Algorithms Chapter 7: Quick Sort.
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]
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.
1 Introduction to Randomized Algorithms Md. Aashikur Rahman Azim.
Quicksort Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
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.
1 SORTING Dan Barrish-Flood. 2 heapsort made file “3-Sorting-Intro-Heapsort.ppt”
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.
1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place.
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
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.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
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.
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
Introduction to Algorithms Jiafen Liu Sept
David Luebke 1 6/3/2016 CS 332: Algorithms Analyzing Quicksort: Average Case.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
COSC 3101A - Design and Analysis of Algorithms 4 Quicksort Medians and Order Statistics Many of these slides are taken from Monica Nicolescu, Univ. of.
David Luebke 1 2/19/2016 Priority Queues Quicksort.
CSC317 1 Quicksort on average run time We’ll prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing.
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.
Computer Sciences Department1. Sorting algorithm 4 Computer Sciences Department3.
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.
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.
CSC 413/513: Intro to Algorithms
Ch 7: Quicksort Ming-Te Chi
Lecture 3 / 4 Algorithm Analysis
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
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.
Order Statistics Comp 122, Spring 2004.
The Selection Problem.
Quicksort and Randomized Algs
Quicksort Quick sort Correctness of partition - loop invariant
Presentation transcript:

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 position and the ending index of A. Output: A permutation of A such that elements are in ascending order

QuickSort Divide: Rearrange the array A[p..r] into two subarrays A[p..q-1] and A[q+1..r] such that each element of A[p..q-1]  A[q] and each element of A[q+1..r] > A[q]. Conquer: Sort the two subarrays by recursive calls to QuickSort. Combine: No work is needed to combine subarrays since they are sorted in-place. To sort the subarray A[p…r] ( initially, p = 1 and r = A.length) :

QuickSort Quicksort(A, p, r) 1. if p < r 2. q = Partition(A, p, r) 3. Quicksort(A, p, q-1) 4. Quicksort(A, q+1, r) Partition(A, p, r) 1. x = A[r] // choose pivot 2. i = p for j = p to r if A[j] ≤ x 5. i = i swap A[i] and A[j] 7. swap A[i+1] and A[r] 8. return i + 1 Initial call: Quicksort(A, 1, A.length) What does Partition do? What is running time of Partition? Note that Partition always selects the last element A[r] in the subarray A[p…r] as the pivot.

Partition Partition(A, p, r) 1. x = A[r] // choose pivot 2. i = p for j = p to r if A[j] ≤ x 5. i = i swap A[i] and A[j] 7. swap A[i+1] and A[r] 8. return i + 1 In line 1, x is set to the value contained in A[r] (the pivot). i is set to index before p, j starts at p j goes through entire array segment from p to r-1; any time A[j] ≤ x, i is incremented and A[i] is exchanged with A[j] When j = r, the value at A[r] is exchanged with the value at A[i+1]. This puts the value in A[r] between the values ≤ x and those > x. So the item at position A[i+1] is in its correct sorted position. The index i+1 is returned because that position no longer needs to be considered.

Partition Partition(A, p, r) 1. x = A[r] // choose pivot 2. i = p for j = p to r if A[j] ≤ x 5. i = i swap A[i] and A[j] 7. swap A[i+1] and A[r] 8. return i + 1 Loop invariant: As Partition executes, the array is divided into 4 regions, some possibly empty, such that 1.All values in A[p…i] are ≤ pivot. 2.All values in A[i+1…j-1] are > pivot. 3.A[r] = pivot. Although not needed as part of the loop invariant, the 4 th region is A[j…r-1], whose entries have not yet been examined, so we don’t know how they compare to the pivot.

Partition Partition(A, p, r) 1. x = A[r] // choose pivot 2. i = p for j = p to r if A[j] ≤ x 5. i = i swap A[i] and A[j] 7. swap A[i+1] and A[r] 8. return i Example on an 8-element array:

Partition Partition(A, p, r) 1. x = A[r] // choose pivot 2. i = p for j = p to r if A[j] ≤ x 5. i = i swap A[i] and A[j] 7. swap A[i+1] and A[r] 8. return i + 1 The index j disappears after step 7 because it is no longer needed once the for loop is exited

Formal Correctness of Partition As the Partition procedure executes, the array is partitioned into four regions, some of which may be empty. Loop invariant: At the beginning of each iteration of the for loop (lines 3-6), for any array index k, 1.If p ≤ k ≤ i, then A[k] ≤ x. 2.If i+1 ≤ k ≤ j-1, then A[k] > x. 3.If k=r, then A[k] = x. The fourth region is A[j…r - 1], whose entries have not yet been examined. Partition(A, p, r) 1. x = A[r] // choose pivot 2. i = p for j = p to r if A[j] ≤ x 5. i = i swap A[i] and A[j] 7. swap A[i+1] and A[r] 8. return i + 1

Partition: Correctness Initialization: Before the loop starts i=0 and j=1, all the conditions of the loop invariant are satisfied because r is the pivot, and the sub-arrays A[p…i] (A[1…0]) and A[i+1…j-1] (A[1…0]) are empty. Maintenance: In each iteration of the loop, either A[j] > x or A[j] ≤ x. In the first case, j is incremented and cond. 2 holds for A[j-1] with no other changes. In the second case, i is incremented, A[i] and A[j] are swapped, and then j is incremented. Cond. 1 holds for A[i] after swap. By the IHOP, the item in A[i] was in A[i+1] during the last iteration, and was > x then, so cond. 2 holds at the end of the iteration. Termination: At termination, j = r and A has been partitioned into 3 sets: A[p…i] pivot, and A[r] = pivot. The last 2 lines of Partition move the pivot element from the highest position in the array to between the 2 sub-arrays by swapping A[i+1] and A[r].

Quicksort Running Time The running time of quicksort depends on the partitioning of subarrays. If the subarrays are balanced, then quicksort can run as fast as mergesort. If the subarrays are unbalanced, then quicksort can run as slowly as insertionsort.

Quicksort Running Time Worst case occurs when subarrays are completely unbalanced, when there are 0 elements in one subarray and n-1 elements in the other. We get the recurrence T(n) = T(n-1) + T(0) + Θ (n) = T(n-1) + Θ (n) = Θ (n 2 ) This is the same running time as the worst case of insertion sort. What input instance would cause the worst-case running time of quicksort to occur? When the array is already sorted in ascending order.

Quicksort Running Time Best case occurs when subarrays are completely balanced and each subarray has ≤ n/2 elements We get the recurrence T(n) = 2T(n/2) + Θ (n) = Θ (nlgn) by Case 2 of the master theorem. This is the same running time as merge sort.

Quicksort Average-case The average case of quicksort’s running time is much closer to the best case than it is to the worst case. Imagine that Partition always produces a 9-to-1 split. Then we get the recurrence T(n) ≤ T(9n/10) + T(n/10) + Θ (n) = O(nlgn) The recursion tree is like the one for T(n) = T(n/3) + T(2n/3) + O(n) Any split of constant proportionality will yield a recursion tree of depth Θ (lgn)

Quicksort Average-case Intuition: Some splits will be close to balanced and others close to unbalanced, so good and bad splits will be randomly distributed in recursion tree. The running time will be (asymptotically) bad only if there are many bad splits in a row. A bad split followed by a good split results in a good partitioning after one extra step.

Quicksort with Good Average Running Time How can we modify Quicksort to get good average case behavior on all inputs? Randomized-Partition(A, p, r) 1. i = Random(p, r) 2. swap A[r] and A[i] 3. return Partition(A, p, r) 2 techniques: 1.randomly permute input prior to running Quicksort. Will produce tree of possible executions, most of them finish fast. 2.choose partition randomly at each iteration instead of choosing element in highest array position. Randomized-Quicksort(A, p, r) 1. if p < r 2. q = Randomized-Partition(A, p, r) 3. Randomized-Quicksort(A, p, q-1) 4. Randomized-Quicksort(A, q+1, r)

Quicksort with Good Average Running Time Randomization of Quicksort stops any specific type of array from causing worst- case behavior. For example, an already-sorted array causes worst-case behavior in non-randomized Quicksort but not in Randomized-Quicksort.

Average-case Analysis of Randomized-Quicksort Rename elements of A as z 1, z 2, …, z n and define the set Z ij to be the set of elements between z i and z j, inclusive. Each pair of elements is compared at most once, because elements are compared only to the pivot and the pivot is not used again. Let X ij = I{ z i is compared to z j } Since each pair is compared at most once, the total # comparisons= Taking the expectations of both sides, use L.5.1 and LoE: But what is Pr{z i is compared to z j }?

Average-case Analysis of Randomized-Quicksort Observations: Numbers in separate partitions are never compared. If the pivot is not z i or z j, these elements are never compared. The probability that z i is compared to z j is the probability that either one is chosen as the pivot. There are j – i + 1 elements and the probability that any particular one is chosen as the pivot is 1/(j – i + 1) Thus Pr{z i is compared to z j } = Pr{z i is the pivot} + Pr{z j is the pivot} = 2/(j-i+1). Substituting this for E[X] and letting k = j - i: So the expected running time of Randomized-Quicksort is O(nlgn) when elements in A are distinct.