Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows:

Similar documents
Lecture 4. Quicksort

Outline. 1 Introduction. Merging and MergeSort. 3 Analysis. 4 Reference

Quicksort. Where Average and Worst Case Differ. S.V. N. (vishy) Vishwanathan. University of California, Santa Cruz

Data Structures and Algorithms CSE 465

Algorithms And Programming I. Lecture 5 Quicksort

Algorithms, CSE, OSU Quicksort. Instructor: Anastasios Sidiropoulos

Week 5: Quicksort, Lower bound, Greedy

Analysis of Algorithms CMPSC 565

Data selection. Lower complexity bound for sorting

Quicksort (CLRS 7) We previously saw how the divide-and-conquer technique can be used to design sorting algorithm Merge-sort

Recommended readings: Description of Quicksort in my notes, Ch7 of your CLRS text.

1 Quick Sort LECTURE 7. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

Analysis of Algorithms. Randomizing Quicksort

Sorting. Chapter 11. CSE 2011 Prof. J. Elder Last Updated: :11 AM

Fundamental Algorithms

Fundamental Algorithms

Advanced Analysis of Algorithms - Midterm (Solutions)

Lecture 5: Loop Invariants and Insertion-sort

CS161: Algorithm Design and Analysis Recitation Section 3 Stanford University Week of 29 January, Problem 3-1.

Solutions. Problem 1: Suppose a polynomial in n of degree d has the form

COMP 250: Quicksort. Carlos G. Oliver. February 13, Carlos G. Oliver COMP 250: Quicksort February 13, / 21

Review Of Topics. Review: Induction

Problem Set 2 Solutions

Algorithms. Quicksort. Slide credit: David Luebke (Virginia)

Outline. Computer Science 331. Cost of Binary Search Tree Operations. Bounds on Height: Worst- and Average-Case

Data Structures and Algorithm Analysis (CSC317) Randomized algorithms

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

Average Case Analysis. October 11, 2011

COMP 382: Reasoning about algorithms

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

Problem. Problem Given a dictionary and a word. Which page (if any) contains the given word? 3 / 26

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

Data Structures and Algorithms

CPS 616 DIVIDE-AND-CONQUER 6-1

CSCI 3110 Assignment 6 Solutions

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

CS 361, Lecture 14. Outline

Central Algorithmic Techniques. Iterative Algorithms

Divide and Conquer Strategy

1 Divide and Conquer (September 3)

Divide-and-conquer: Order Statistics. Curs: Fall 2017

Divide and Conquer. Chapter Overview. 1.2 Divide and Conquer

Kartsuba s Algorithm and Linear Time Selection

Sorting algorithms. Sorting algorithms

Introduction to Divide and Conquer

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Linear Selection and Linear Sorting

Lecture 2: Asymptotic Notation CSCI Algorithms I

Sorting Algorithms. We have already seen: Selection-sort Insertion-sort Heap-sort. We will see: Bubble-sort Merge-sort Quick-sort

Inf 2B: Sorting, MergeSort and Divide-and-Conquer

Divide and Conquer CPE 349. Theresa Migler-VonDollen

CS 161 Summer 2009 Homework #2 Sample Solutions

Sorting DS 2017/2018

Data Structures and Algorithms Chapter 2

CSE 421, Spring 2017, W.L.Ruzzo. 8. Average-Case Analysis of Algorithms + Randomized Algorithms

COMP Analysis of Algorithms & Data Structures

COL106: Data Structures and Algorithms (IIT Delhi, Semester-II )

Divide and Conquer Algorithms

Analysis of Algorithms - Using Asymptotic Bounds -

CE 221 Data Structures and Algorithms. Chapter 7: Sorting (Insertion Sort, Shellsort)

CSE 312, Winter 2011, W.L.Ruzzo. 8. Average-Case Analysis of Algorithms + Randomized Algorithms

Introduction to Algorithms and Data Structures. Markus Bläser Saarland University Draft Thursday 22, 2015 and forever

Recap: Prefix Sums. Given A: set of n integers Find B: prefix sums 1 / 86

COMP Analysis of Algorithms & Data Structures

Divide-and-conquer. Curs 2015

COMP Analysis of Algorithms & Data Structures

Introduction to Algorithms

Data Structures and Algorithm Analysis (CSC317) Randomized Algorithms (part 3)

Quick Sort Notes , Spring 2010

Introduction to Randomized Algorithms: Quick Sort and Quick Selection

Quicksort algorithm Average case analysis

Algorithm Design and Analysis

Midterm Exam. CS 3110: Design and Analysis of Algorithms. June 20, Group 1 Group 2 Group 3

5. DIVIDE AND CONQUER I

1. [10 marks] Consider the following two algorithms that find the two largest elements in an array A[1..n], where n >= 2.

Module 1: Analyzing the Efficiency of Algorithms

Partition and Select

ITEC2620 Introduction to Data Structures

Big O 2/14/13. Administrative. Does it terminate? David Kauchak cs302 Spring 2013

Selection and Adversary Arguments. COMP 215 Lecture 19

CS Analysis of Recursive Algorithms and Brute Force

Advanced Analysis of Algorithms - Homework I (Solutions)

COMP 9024, Class notes, 11s2, Class 1

CIS 121. Analysis of Algorithms & Computational Complexity. Slides based on materials provided by Mary Wootters (Stanford University)

Extended Algorithms Courses COMP3821/9801

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

Searching. Sorting. Lambdas

CS173 Running Time and Big-O. Tandy Warnow

Algorithms. Jordi Planes. Escola Politècnica Superior Universitat de Lleida

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005

Lecture 6 September 21, 2016

Algorithms and Data Structures

Midterm 1 for CS 170

CMPT 307 : Divide-and-Conqer (Study Guide) Should be read in conjunction with the text June 2, 2015

CSE 613: Parallel Programming. Lecture 8 ( Analyzing Divide-and-Conquer Algorithms )

CSE 613: Parallel Programming. Lecture 9 ( Divide-and-Conquer: Partitioning for Selection and Sorting )

Divide and conquer. Philip II of Macedon

CS 4407 Algorithms Lecture 3: Iterative and Divide and Conquer Algorithms

CSE 591 Homework 3 Sample Solutions. Problem 1

Transcription:

Outline Computer Science 331 Quicksort Mike Jacobson Department of Computer Science University of Calgary Lecture #28 1 Introduction 2 Randomized 3 Quicksort Deterministic Quicksort Randomized Quicksort 4 5 References Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 1 / 24 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 2 / 24 Introduction Introduction Idea Introduction Quicksort: A recursive Divide and Conquer sorting algorithm A simple deterministic version uses Θ(n 2 ) operations to sort an array of size n in the worst case Θ(n log n) operations on average, assuming all relative orderings of the (distinct) input are equally likely The expected number of operations used by a randomized version is in O(n log n) for any input array of size n Reference: Introduction to Algorithms, Chapter 7. This textbook discusses a different version of this algorithm. 1 Choose an element x and reorder the array as follows: x is in the correct spot if the array was sorted elements < x are to the left of x in the array elements > x are to the right of x in the array 2 Recursively sort subarray of elements to the left of x 3 Recursively sort subarray of elements to the right of x Step 1 is the key to this method being efficient. Issues: Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 3 / 24 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 4 / 24

A procedure that is called repeatedly by the main Quicksort algorithm (Step 1 in the previous description) Objective: Reorder the values in a given portion of the array so that all values less than or equal to some pivot element are at the beginning, and all values greater than this pivot element are at the end. Return the location of the pivot element as output Number of operations used is linear in the length of the part of the array that is being processed Idea: Pivot element used is the last element in the part of the array being processed. Other versions of this algorithm use the first element instead. Sweep from left to right over the array, exchanging elements as needed, so that values less than or equal to the pivot element are all located before values that are greater than the pivot element, in the part of the array that has been processed Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 5 / 24 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 6 / 24 Pseudocode Example DPartition(A, p, r) {Assumption: 0 p r < length(a)} x = A[r] i = p 1; j = p while j < r do if A[j] x then i = i + 1 Swap: tmp = A[i]; A[i] = A[j]; A[j] = tmp end if j = j + 1 end while Swap: tmp = A[i + 1]; A[i + 1] = A[r]; A[r] = tmp return i + 1 Consider the execution of DPartition(A, 3, 10) for A as follows: 2 6 4 1 7 3 0 5 Using x = A[10] = 5 as the pivot. Initially i = 2, j = 3. j = 3 : j = 4 : Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 7 / 24 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 8 / 24

Example (cont.) Example (cont.) j = 5 : j = 8 : j = 6 : j = 9 : j = 7 : j = 10 : Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 9 / 24 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 10 / 24 Partial Correctness: Loop Invariant Suppose p and r are integers such that 0 p r < length(a). If k 0 and the for-loop is executed at least k times, then the following conditions are satisfied at the beginning of the k th execution of the loop body: 1 j Z, p j r 1, and j = p + k. 2 i Z, and p 1 i j 1. 3 The following hold for each integer l such that p l r: If p l i then A[l] x. If i + 1 l j 1 then A[l] > x. If l = r then A[l] = x. 4 Entries of A are reordered but otherwise unchanged. Note: At the end of this execution of the loop body, either j = i + 1, or j > i + 1 and A[j] > x as well. Partial Correctness: Application of Loop Invariant Once again, suppose that p and r are integers such that 0 p r < length(a). Let x be the value that was stored at location r of A before the program was called. If the program halts then the following conditions are satisfied on termination (that is, when the return statement is executed): 1 p 1 i r 1. 2 The following relationships hold for each integer l such that p l r: If p l i then A[l] x. If l = i + 1 then A[l] = x. If i + 2 l r then A[l] > x. 3 Entries of A are reordered but otherwise unchanged. Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 11 / 24 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 12 / 24

Randomized Termination and Efficiency Randomized Loop Variant: r j Justification: Application: The initial value of the loop variant is r p Therefore the loop body is executed exactly r p times. Each execution of the loop body requires (at most) a constant number of operations. Therefore the cost to execute the loop is in O(r p). Since the rest of the program only uses a constant number of operations, it is clear that the program terminates and that it uses O(r p) operations in the worst case. Idea: Choose the pivot element randomly from the set of values in the part of the array to be processed. Then proceed as before. RPartition(A, p, r) {Assumption: 0 p r < length(a)} Choose i randomly and uniformly from the set of integers between p and r (inclusive). Exchange: tmp = A[i]; A[i] = A[r]; A[r] = tmp return DPartition(A, p, r) Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 13 / 24 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 14 / 24 Randomized Randomized : Deterministic Quicksort Quicksort Deterministic Quicksort Suppose p and r are integers such that 0 p r < length(a) and that RPartition is called with inputs A, p and r. This algorithm terminates using O(r p) operations. Let q be the value that is returned on termination. Then the following conditions hold on termination. p q r. If l Z and p l < q then A[l] A[q]. If l Z and q < l r then A[l] > A[q]. If the entries on A are distinct then q = i with probability r p + 1 for each integer i between p and r (inclusive). Entries of A are reordered but otherwise unchanged. Idea: Partition the array, then recursively sort the pieces before and after the pivot element. Call quicksort(a, 0, length(a) 1) to sort A: quicksort(a, p, r) if p < r then q = DPartition(A, p, r) quicksort(a, p, q 1) quicksort(a, q + 1, r) end if Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 15 / 24 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 16 / 24

Randomized Quicksort Quicksort Randomized Quicksort Worst-Case of Deterministic Quicksort Idea: Same as deterministic Quicksort, except that randomized partitioning is used. Call RQuicksort(A, 0, length(a) 1) to sort A: RQuicksort(A, p, r) if p < r then q = RPartition(A, p, r) RQuicksort(A, p, q 1) RQuicksort(A, q + 1, r) end if Theorem 1 Let T (n) be the number of steps used by QuickSort to sort an array of length n in the worst case. Then c 0 if n 1, T (n) c 1 n + max (T (k) + T (n 1 k)) if n 2. 0 k n 1 Justification: Base case requires constant # of steps. General case: Application: This recurrence can be used to prove that T (n) O(n 2 ) using induction on k. Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 17 / 24 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 18 / 24 Worst-Case of Deterministic Quicksort Average-Case of Deterministic Quicksort Consider an application of this algorithm when the input is an array A with n distinct entries Theorem 2 If Deterministic Quicksort is applied to an array of length n whose entries are already sorted then this algorithm uses Ω(n 2 ) steps. Method of Proof: Induction on n, once again. Conclusion: Deterministic Quicksort uses Θ(n 2 ) to sort an array of length n in the worst case. Consider a binary search tree T storing the same values, with the partition element at the root the left subtree formed by considering the application of the algorithm to the left subarray the right subtree formed by considering the application of the algorithm to the right subarray Useful Property: The number of steps used by the algorithm is at most for some positive constant c cn(height(t ) + 1) Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 19 / 24 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 20 / 24

Average-Case of Deterministic Quicksort of Randomized Quicksort Assumption for : Entries of A are distinct and all n! relative orderings of these inputs are equally likely Useful Property: The corresponding binary search trees T are generated with the probability distribution discussed in the Average Case of Binary Search Trees. Bounds on expected height of trees from those notes can now be applied. The previous analysis can be modified to establish that the worst-case expected cost of Randomized Quicksort to sort an array with distinct entries is in O(n log n) as well. Note: it is possible to obtain a worst-case running time of Θ(n log n) careful (but deterministic) selection of the pivot (see Introduction to Algorithms, Chapter 9.3) Conclusion: The expected cost of Quicksort is in O(n log n) if the above assumption for analysis is valid. Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 21 / 24 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 22 / 24 An Annoying Problem References References An Annoying Problem: Both versions of Quicksort, given above, use Θ(n 2 ) operations to sort an array of length n if the array contains n copies of the same value! The different version of Quicksort found in the textbook has the same problem! The tutorial exercises on quicksort discuss a modification to partition that addresses this problem. References: Cormen, Leiserson, Rivest and Stein Introduction to Algorithms, Second Edition Chapter 7 includes more details, including a complete analysis of the version of Quicksort presented here Textbook Section 10.9 presents a Quicksort algorithm and analysis. Two different partition algorithms with the same asymptotic cost are presented. Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 23 / 24 Mike Jacobson (University of Calgary) Computer Science 331 Lecture #28 24 / 24