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

Similar documents
Algorithms And Programming I. Lecture 5 Quicksort

Lecture 4. Quicksort

Data Structures and Algorithms CSE 465

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

Algorithms, CSE, OSU Quicksort. Instructor: Anastasios Sidiropoulos

Week 5: Quicksort, Lower bound, Greedy

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

Analysis of Algorithms CMPSC 565

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

Analysis of Algorithms. Randomizing Quicksort

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

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

Data Structures and Algorithm Analysis (CSC317) Randomized algorithms

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

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

Fundamental Algorithms

Fundamental Algorithms

Review Of Topics. Review: Induction

Quick Sort Notes , Spring 2010

Searching. Sorting. Lambdas

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

CS 161 Summer 2009 Homework #2 Sample Solutions

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

ITEC2620 Introduction to Data Structures

Chunksort: A Generalized Partial Sort Algorithm

Asymptotic Running Time of Algorithms

Quicksort. Recurrence analysis Quicksort introduction. November 17, 2017 Hassan Khosravi / Geoffrey Tien 1

1 Divide and Conquer (September 3)

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

Quicksort algorithm Average case analysis

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures

data structures and algorithms lecture 2

CSCI 3110 Assignment 6 Solutions

COMP Analysis of Algorithms & Data Structures

Lecture 20: Analysis of Algorithms

10: Analysis of Algorithms

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

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

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

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

Partition and Select

Central Algorithmic Techniques. Iterative Algorithms

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

CSE 21 Practice Exam for Midterm 2 Fall 2017

Data selection. Lower complexity bound for sorting

CS361 Homework #3 Solutions

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

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

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

Bucket-Sort. Have seen lower bound of Ω(nlog n) for comparisonbased. Some cheating algorithms achieve O(n), given certain assumptions re input

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

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

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

Divide and conquer. Philip II of Macedon

Multi-Pivot Quicksort: Theory and Experiments

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

Divide-and-conquer. Curs 2015

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

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

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

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

Divide and Conquer Strategy

CS 4104 Data and Algorithm Analysis. Recurrence Relations. Modeling Recursive Function Cost. Solving Recurrences. Clifford A. Shaffer.

4.1, 4.2: Analysis of Algorithms

1. Basic Algorithms: Bubble Sort

1 Terminology and setup

COMP 250 Fall Midterm examination

5. DIVIDE AND CONQUER I

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

Asymptotic Analysis. Thomas A. Anastasio. January 7, 2004

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Introduction to Divide and Conquer

Lecture 10: Big-Oh. Doina Precup With many thanks to Prakash Panagaden and Mathieu Blanchette. January 27, 2014

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

Computational Complexity

1. Basic Algorithms: Bubble Sort

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

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

CS 470/570 Divide-and-Conquer. Format of Divide-and-Conquer algorithms: Master Recurrence Theorem (simpler version)

Linear Time Selection

CS 310 Advanced Data Structures and Algorithms

CPS 616 DIVIDE-AND-CONQUER 6-1

Lecture 2: Asymptotic Notation CSCI Algorithms I

Formal Languages. We ll use the English language as a running example.

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

Asymptotic Algorithm Analysis & Sorting

CS173 Running Time and Big-O. Tandy Warnow

Algorithms. Algorithms 2.2 MERGESORT. mergesort bottom-up mergesort sorting complexity divide-and-conquer ROBERT SEDGEWICK KEVIN WAYNE

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

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Formal Languages. We ll use the English language as a running example.

Analysis of Approximate Quickselect and Related Problems

Data Structures and Algorithms

Advanced Analysis of Algorithms - Midterm (Solutions)

Lecture 2: MergeSort. CS 341: Algorithms. Thu, Jan 10 th 2019

Divide and Conquer Algorithms

5. DIVIDE AND CONQUER I

Concrete models and tight upper/lower bounds

Problem Set 2 Solutions

Transcription:

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

1 1 https://xkcd.com/1667/ Carlos G. Oliver COMP 250: Quicksort February 13, 2018 2 / 21

Quicksort Invented by Sir Tony Hoare in 1959 while on exchange in the Soviet Union working on machine translation. Tony needed a good way to sort Russian words alphabetically to efficiently look them up in a dictionary, so he invented QuickSort. I think Quicksort is the only really interesting algorithm that I ever developed. Sir Tony Hoare Fun fact: Tony Hoare invented the null pointer, and he is sorry. 2 https://en.wikipedia.org/wiki/tony_hoare Carlos G. Oliver COMP 250: Quicksort February 13, 2018 3 / 21 2

Main idea in QuickSort Idea: why don t we start by finding the final position of a given element in the sorted array? When is an element in its final position of a sorted array? For any element x in a sorted array, all elements before it (to the left) are smaller, and all elements to the right are larger. <= x x >= x Goal: find the position in the array such that the above condition holds linear time. Split the array at x and sort the resulting subarrays. QuickSort is an in-place sorting algorithm. i.e. does not require extra memory for execution (like the temporary array in MergeSort). Carlos G. Oliver COMP 250: Quicksort February 13, 2018 4 / 21

Quicksort (simple, not in place) Algorithm 1: QuickSort(A) Result: Sorted array A. 1 if len(a) 1 then 2 return A 3 else 4 x = A.removeFirst() 5 list1 = A.getElementsLessThan(x) 6 list2 = A.getElementsNotLessThan(x) 7 list1 = QuickSort(list1) 8 list2 = QuickSort(list2) 9 return concatenate(list1, x, list2) Carlos G. Oliver COMP 250: Quicksort February 13, 2018 5 / 21

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

QuickSort in place pseudocode If we re clever about how we get arrange the elements around the pivot we can do without temporary arrays. This is the job of the partition function Algorithm 2: QuickSort(A, p, q) Result: Sorted array A between indices p and q 1 if p q then 2 x Partition(A, p, q) 3 QuickSort(A, p, x 1) 4 QuickSort(A, x + 1, q) 5 return Carlos G. Oliver COMP 250: Quicksort February 13, 2018 7 / 21

Partition The job of partition is for some pivot value (usually we take the first or last element), arrange all elements such that elements less than the pivot are to the left of the pivot, and conversely for the right. partition places the pivot in the correct position and returns that position. This procedure runs in linear time. i.e. Θ(n) Carlos G. Oliver COMP 250: Quicksort February 13, 2018 8 / 21

Parition pseudocode (Version 1) Algorithm 3: Partition(A, p, q) Result: Index left and rearranges elements of A such that i < left, A[i] A[left] and k > left, A[k] A[left]. 1 x A[q] 2 left p 3 right q 1 4 while left right do 5 while left right A[left] < x do 6 left left + 1 7 while left right A[right] x do 8 right right 1 9 if left < right then 10 swap(a[left], A[right]) 11 swap(a[p], A[left]) 12 return left Carlos G. Oliver COMP 250: Quicksort February 13, 2018 9 / 21

Partition (Version 1) Loop invariant intuition At each step we ensure that elements to the right of right and left of left are on the correct side of the pivot. < x? > x x p left right q Carlos G. Oliver COMP 250: Quicksort February 13, 2018 10 / 21

Partition example 6 3 7 3 2 5 7 5 6 3 7 3 2 5 7 5 swap(6,2) 2 3 7 3 6 5 7 5 swap(7,3) 2 3 3 7 6 5 7 5 swap(7,5) 2 3 4 5 6 5 7 7 Carlos G. Oliver COMP 250: Quicksort February 13, 2018 11 / 21

Back to the big picture Carlos G. Oliver COMP 250: Quicksort February 13, 2018 12 / 21

Parition pseudocode (Version 2) Algorithm 4: Partition(A, p, q) Result: Index left and rearranges elements of A such that i < left, A[i] A[left] and k > left, A[k] A[left]. 1 x A[q] 2 left p 3 for right p + 1 to q do 4 if A[right] x then 5 left left + 1 6 swap(a[left-1], A[right]) 7 swap(a[p], A[left]) 8 return left Carlos G. Oliver COMP 250: Quicksort February 13, 2018 13 / 21

Partition (Version 2) Loop Invariant < x > x? x p left right q Carlos G. Oliver COMP 250: Quicksort February 13, 2018 14 / 21

Demo of partition (version 2) 2 3 7 4 6 8 7 5 swap(2,3) 3 2 7 4 6 8 7 5 swap(2,4) 3 4 7 2 6 8 7 5 swap(2,7) 3 4 2 7 6 8 7 5 swap(7,5) 3 4 2 5 6 8 7 7 Carlos G. Oliver COMP 250: Quicksort February 13, 2018 15 / 21

QuickSort Time Complexity: Worst case Worst case of QuickSort occurs on sorted (or inversely sorted) arrays. All elements will fall to the same side of the pivot so the splits decrease recursive calls by 1 only. T worst (n) Θ(n 2 ) 3 3 https://ka-perseus-images.s3.amazonaws.com/7da2ac32779bef669a6f05decb62f219a9132158.png Carlos G. Oliver COMP 250: Quicksort February 13, 2018 16 / 21

QuickSort Time Complexity: Worst case recurrence T worst (n) = cn + T worst (n 1) (1) = cn + c(n 1) + T (n 2) (2) = cn + c(n 1) + c(n 2) + T (n 3) (3) = cn + c(n 1) + c(n 2) + c(n k 1) + T (n 1) (k) n = c, k + T (0), when n k = 1 = c k=0 n(n + 1) 2 Θ(n 2 ) We do a linear amount of work for partition (cn) plus QuickSort on the rest of the array T (n 1) (1) Carlos G. Oliver COMP 250: Quicksort February 13, 2018 17 / 21

QuickSort Time Complexity: Best case Best case is when the pivot divides the array in two each time. T best (n) Θ(n log n) 4 4 https://ka-perseus-images.s3.amazonaws.com/21cd0d70813845d67fbb11496458214f90ad7cb8.png Carlos G. Oliver COMP 250: Quicksort February 13, 2018 18 / 21

QuickSort Time Complexity: Best case recurrence (n ) T best (n) = cn + 2T best Θ(n log n) (2) 2 We do a linear amount of work for partition (cn) plus QuickSort on the rest of the array T (n 1) Recurrence is the same as MergeSort. Carlos G. Oliver COMP 250: Quicksort February 13, 2018 19 / 21

Quicksort Considerations Average case: if the array is in random order, it is split in roughly even parts: t average (n) Θ(n log n) Advantage over MergeSort Constants hidden in O(n log n) smaller than in MergeSort faster by constant factor. QuickSort is easy to do without additional memory. Good for large lists. SelectionSort and InsertionSort are in-place: all we are doing is moving elements around the array There are strategies to picking pivots to avoid bad running times. Carlos G. Oliver COMP 250: Quicksort February 13, 2018 20 / 21

Good to know 1 + 2 + 3 + 4 + 5 +... + k = 1 + 2 + 4 +... + 2 k = 1 + x + x 2 + x 3 + x 4 +... + x k = n k = k=1 n(n + 1) 2 n 2 k = 2 n+1 1 k=1 n k=1 x k = x k+1 1 x 1 (3) Carlos G. Oliver COMP 250: Quicksort February 13, 2018 21 / 21