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

Similar documents
Algorithms, CSE, OSU Quicksort. Instructor: Anastasios Sidiropoulos

Lecture 4. Quicksort

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

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

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

Analysis of Algorithms. Randomizing Quicksort

Recurrence Relations

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

Week 5: Quicksort, Lower bound, Greedy

Data Structures and Algorithms CSE 465

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

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

Data Structures and Algorithm Analysis (CSC317) Randomized algorithms

Algorithms And Programming I. Lecture 5 Quicksort

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

Analysis of Algorithms CMPSC 565

Fundamental Algorithms

Fundamental Algorithms

Searching. Sorting. Lambdas

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

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

COMP Analysis of Algorithms & Data Structures

CS 161 Summer 2009 Homework #2 Sample Solutions

Divide and Conquer Strategy

Time complexity analysis

Divide and conquer. Philip II of Macedon

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

Data selection. Lower complexity bound for sorting

Data Structures and Algorithms

Average Case Analysis. October 11, 2011

Lecture 5: Loop Invariants and Insertion-sort

Divide-and-conquer. Curs 2015

Review Of Topics. Review: Induction

1 Divide and Conquer (September 3)

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

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

Quicksort algorithm Average case analysis

Divide & Conquer. Jordi Cortadella and Jordi Petit Department of Computer Science

CSCI 3110 Assignment 6 Solutions

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

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Divide & Conquer. Jordi Cortadella and Jordi Petit Department of Computer Science

Partition and Select

Lecture 2: Asymptotic Notation CSCI Algorithms I

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

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

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

Algorithms and Their Complexity

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

Algorithms and Data Structures

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

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

Advanced Analysis of Algorithms - Midterm (Solutions)

Chunksort: A Generalized Partial Sort Algorithm

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

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

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

On Partial Sorting. Conrado Martínez. Univ. Politècnica de Catalunya, Spain

COMP 382: Reasoning about algorithms

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

ITEC2620 Introduction to Data Structures

1. Basic Algorithms: Bubble Sort

Central Algorithmic Techniques. Iterative Algorithms

Divide and Conquer Algorithms

Problem Set 2 Solutions

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

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

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

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

CS Analysis of Recursive Algorithms and Brute Force

Divide-Conquer-Glue. Divide-Conquer-Glue Algorithm Strategy. Skyline Problem as an Example of Divide-Conquer-Glue

CS483 Design and Analysis of Algorithms

Randomized algorithms. Inge Li Gørtz

data structures and algorithms lecture 2

Sorting algorithms. Sorting algorithms

An Introduction to Randomized algorithms

CPS 616 DIVIDE-AND-CONQUER 6-1

CIS 121 Data Structures and Algorithms with Java Spring Big-Oh Notation Monday, January 22/Tuesday, January 23

Sorting DS 2017/2018

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

Design and Analysis of Algorithms PART II-- Median & Runtime Analysis Recorded by Chandrasekar Vijayarenu and Shridharan Muthu.

Data Structures and Algorithms Chapter 2

Introduction to Randomized Algorithms: Quick Sort and Quick Selection

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

CS 344 Design and Analysis of Algorithms. Tarek El-Gaaly Course website:

On my honor I affirm that I have neither given nor received inappropriate aid in the completion of this exercise.

Concrete models and tight upper/lower bounds

Algorithm Design CS 515 Fall 2015 Sample Final Exam Solutions

1 Terminology and setup

Chapter 5. Divide and Conquer CLRS 4.3. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Divide and Conquer. Andreas Klappenecker. [based on slides by Prof. Welch]

Quick Sort Notes , Spring 2010

Asymptotic Algorithm Analysis & Sorting

Algorithms Test 1. Question 1. (10 points) for (i = 1; i <= n; i++) { j = 1; while (j < n) {

CS 361, Lecture 14. Outline

Multi-Pivot Quicksort: Theory and Experiments

Lecture 22: Multithreaded Algorithms CSCI Algorithms I. Andrew Rosenberg

Bin Sort. Sorting integers in Range [1,...,n] Add all elements to table and then

Divide-Conquer-Glue Algorithms

5. DIVIDE AND CONQUER I

Transcription:

Quicksort Where Average and Worst Case Differ S.V. N. (vishy) Vishwanathan University of California, Santa Cruz vishy@ucsc.edu February 1, 2016 S.V. N. Vishwanathan (UCSC) CMPS101 1 / 28

Basic Idea Outline 1 Basic Idea 2 Worst Case Analysis 3 Average Case Analysis S.V. N. Vishwanathan (UCSC) CMPS101 2 / 28

Basic Idea Divide and Conquer Divide: Choose a random pivot a A and split the array into A < and A >. Conquer: Combine: position) Sort A < and A > by calling quicksort Nothing to do! (note that a is already in its correct S.V. N. Vishwanathan (UCSC) CMPS101 3 / 28

Basic Idea Pseudocode 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) S.V. N. Vishwanathan (UCSC) CMPS101 4 / 28

Basic Idea Partition S.V. N. Vishwanathan (UCSC) CMPS101 5 / 28

Basic Idea Partition partition(a, p, r) 1 x = A[r] 2 i = p 1 3 for j = p to r 1 4 if A[j] x 5 i = i + 1 6 exchange A[i] with A[j] 7 exchange A[i + 1] with A[r] 8 return i + 1 S.V. N. Vishwanathan (UCSC) CMPS101 6 / 28

Basic Idea Loop Invariant At the beginning of each iteration of the loop of 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. Indices between j and r 1 have no particular relationship to x S.V. N. Vishwanathan (UCSC) CMPS101 7 / 28

Basic Idea In Pictures S.V. N. Vishwanathan (UCSC) CMPS101 8 / 28

Basic Idea Initialization i = p 1 and j = p No values lie between i + 1 and j 1 First two conditions are trivially satisfied x = A[r] in line 1 satisfies the third condition S.V. N. Vishwanathan (UCSC) CMPS101 9 / 28

Basic Idea Maintenance S.V. N. Vishwanathan (UCSC) CMPS101 10 / 28

Basic Idea Maintenance Case 1: A[j] > x increment j Condition 2 now holds for A[j 1] Other entries remain unchanged Case 2: A[j] x Increment i and swap A[i] with A[j] This satisfies condition 1 Increment j Now A[j 1] > x, this satisfies condition 3 S.V. N. Vishwanathan (UCSC) CMPS101 11 / 28

Basic Idea Termination j = r so every element in the array is either in A or A > S.V. N. Vishwanathan (UCSC) CMPS101 12 / 28

Worst Case Analysis Outline 1 Basic Idea 2 Worst Case Analysis 3 Average Case Analysis S.V. N. Vishwanathan (UCSC) CMPS101 13 / 28

Worst Case Analysis Worst Case Time Complexity Partitioning procedure produces one sub-problem with n 1 elements and one with 0 elements Recurrence relationship Therefore T (n) = O(n 2 ) T (n) = T (n 1) + T (0) + Θ(n) = T (n 1) + Θ(n) Worst case occurs when the array is already sorted! S.V. N. Vishwanathan (UCSC) CMPS101 14 / 28

Worst Case Analysis Best Case Time Complexity Partitioning procedure produces one sub-problem with n/2 elements and one with n/2 1 elements Recurrence relationship Therefore T (n) = Ω(n log(n)) T (n) = 2T (n/2) + Θ(n) S.V. N. Vishwanathan (UCSC) CMPS101 15 / 28

Average Case Analysis Outline 1 Basic Idea 2 Worst Case Analysis 3 Average Case Analysis S.V. N. Vishwanathan (UCSC) CMPS101 16 / 28

Average Case Analysis Average Case Suppose each permutation of an array is equally likely as an input to quicksort What is the expected time complexity? Turns out the average case time complexity of quicksort is O(n log n) S.V. N. Vishwanathan (UCSC) CMPS101 17 / 28

Average Case Analysis What about Imbalanced Splits? Even if the partitioning procedure produces one sub-problem with 9n/10 elements and one with n/10 elements Recurrence relationship T (n) = T (9n/10) + T (n/10) + Θ(n) Still evaluates to T (n) = O(n log(n)) S.V. N. Vishwanathan (UCSC) CMPS101 18 / 28

Average Case Analysis Proof by Picture S.V. N. Vishwanathan (UCSC) CMPS101 19 / 28

Average Case Analysis Randomizing Pivot Selection randomized-partition(a, p, r) 1 i = random(p, r) 2 exchange A[r] with A[i] 3 return partition(a, p, r) Call randomized-partition(a, p, r) instead of partition(a, p, r) S.V. N. Vishwanathan (UCSC) CMPS101 20 / 28

Average Case Analysis Bounding Runtime Lemma Let X be the number of comparisons performed in line 4 of partition over the entire execution of quicksort on an n-element array. Then the running time of quicksort is O(n + X ) Proof There are at most n calls to partition Each call takes O(1) time to setup The loop in lines 3 6 is the only place where we make a comparison S.V. N. Vishwanathan (UCSC) CMPS101 21 / 28

Average Case Analysis Terminology Rename elements of A as z 1, z 2,..., z n, with z i s sorted Define Z ij = {z i, z i+1,..., z j } to be the set of elements between z i and z j, inclusive Let X ij be an indicator variable Therefore X ij = I {z i is compared to z j } X = n 1 n i=1 j=i+1 X ij S.V. N. Vishwanathan (UCSC) CMPS101 22 / 28

Average Case Analysis Expectations n 1 E [X ] = E = = n 1 n i=1 j=i+1 n i=1 j=i+1 n 1 n i=1 j=i+1 X ij E [X ij ] Pr {z i is compared to z j }. S.V. N. Vishwanathan (UCSC) CMPS101 23 / 28

Average Case Analysis Observation - I If z i < x < z j is chosen as a pivot then z i and z j are never compared S.V. N. Vishwanathan (UCSC) CMPS101 24 / 28

Average Case Analysis Observation - II If z i is chosen as a pivot before any other item in Z ij, then z i is compared with each item in Z ij Same holds for z j S.V. N. Vishwanathan (UCSC) CMPS101 25 / 28

Average Case Analysis Probability of Comparison Pr {z i is compared to z j } = Pr {z i or z j is first pivot chosen from Z ij } = Pr {z i or is first pivot chosen from Z ij } + Pr {z j or is first pivot chosen from Z ij } 1 = j i + 1 + 1 j i + 1 2 = j i + 1 S.V. N. Vishwanathan (UCSC) CMPS101 26 / 28

Average Case Analysis Expectations E [X ] = n 1 n i=1 j=i+1 n 1 n i = i=1 k=1 n 1 n i=1 k=1 n 1 2 j i + 1 2 k + 1 2 k O(log n) i=1 = O(n log n) S.V. N. Vishwanathan (UCSC) CMPS101 27 / 28

Average Case Analysis Questions? S.V. N. Vishwanathan (UCSC) CMPS101 28 / 28