Algorithms CSCI 235, Spring 2019 Lecture 17 Quick Sort II

Slides:



Advertisements
Similar presentations
David Luebke 1 4/22/2015 CS 332: Algorithms Quicksort.
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Using Divide and Conquer for Sorting
Spring 2015 Lecture 5: QuickSort & Selection
Chapter 7: Sorting Algorithms
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.
Lecture 5: Linear Time Sorting Shang-Hua Teng. Sorting Input: Array A[1...n], of elements in arbitrary order; array size n Output: Array A[1...n] of the.
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
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.
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.
Analysis of Algorithms CS 477/677
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.
Princeton University COS 423 Theory of Algorithms Spring 2001 Kevin Wayne Average Case Analysis.
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.
COSC 3101A - Design and Analysis of Algorithms 6 Lower Bounds for Sorting Counting / Radix / Bucket Sort Many of these slides are taken from Monica Nicolescu,
David Luebke 1 2/19/2016 Priority Queues Quicksort.
Sorting Lower Bounds n Beating Them. Recap Divide and Conquer –Know how to break a problem into smaller problems, such that –Given a solution to the smaller.
M. Böhlen and R. Sebastiani9/20/20161 Data Structures and Algorithms Roberto Sebastiani
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 4.
Lower Bounds & Sorting in Linear Time
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Analysis of Algorithms CS 477/677
David Kauchak cs062 Spring 2010
Order Statistics.
Quick Sort Divide: Partition the array into two sub-arrays
Order Statistics Comp 122, Spring 2004.
Introduction to Algorithms Prof. Charles E. Leiserson
Algorithms CSCI 235, Fall 2017 Lecture 13 Elementary Sorts II
Algorithms and Data Structures Lecture VI
Chapter 7 Sorting Spring 14
Algorithms CSCI 235, Fall 2017 Lecture 16 Quick Sort Read Ch. 7
QuickSort QuickSort Best, Worst Average Cases K-th Ordered Statistic
CSC 413/513: Intro to Algorithms
Order Statistics(Selection Problem)
(2,4) Trees 11/15/2018 9:25 AM Sorting Lower Bound Sorting Lower Bound.
Sorting We have actually seen already two efficient ways to sort:
Advanced Sorting Methods: Shellsort
Quick Sort (11.2) CSE 2011 Winter November 2018.
Eitan Netzer Tutorial 4-5
CO 303 Algorithm Analysis And Design Quicksort
Ch 7: Quicksort Ming-Te Chi
Lecture 3 / 4 Algorithm Analysis
CS200: Algorithm Analysis
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
CS 3343: Analysis of Algorithms
Lower Bounds & Sorting in Linear Time
CS 583 Analysis of Algorithms
CS 3343: Analysis of Algorithms
(2,4) Trees 2/28/2019 3:21 AM Sorting Lower Bound Sorting Lower Bound.
CS 332: Algorithms Quicksort David Luebke /9/2019.
Algorithms: Design and Analysis
Chapter 7 Quicksort.
Algorithms CSCI 235, Spring 2019 Lecture 18 Linear Sorting
Algorithms CSCI 235, Spring 2019 Lecture 20 Order Statistics II
Algorithms CSCI 235, Spring 2019 Lecture 16 Quick Sort Read Ch. 7
Order Statistics Comp 122, Spring 2004.
Chapter 8: Overview Comparison sorts: algorithms that sort sequences by comparing the value of elements Prove that the number of comparison required to.
CS200: Algorithm Analysis
CS 583 Analysis of Algorithms
The Selection Problem.
Algorithms CSCI 235, Spring 2019 Lecture 19 Order Statistics
Design and Analysis of Algorithms
CSE 332: Sorting II Spring 2016.
Algorithms CSCI 235, Spring 2019 Lecture 26 Midterm 2 Review
Algorithms CSCI 235, Spring 2019 Lecture 13 Elementary Sorts II
Advanced Sorting Methods: Shellsort
Presentation transcript:

Algorithms CSCI 235, Spring 2019 Lecture 17 Quick Sort II

Pseudocode for QuickSort Quick-Sort(A) QSort(A, 1, length[A]) QSort(A, lo, hi) if lo < hi p = Partition(A, lo, hi) QSort(A, lo, p) QSort(A, p + 1, hi) Partition(A, lo, hi) {Rearrange A into non-empty segments A[lo..p] and A[p+1..hi] such that all elements in the left segment are less than all elements in the right one. Return partitioning index p.}

Another uneven split Suppose every split gives 2 arrays whose ratio in size is 99:1 T(n) = T(99n/100) + T(n/100) + n cost of partition We will work this out in class.

Alternating "good" and "bad" partitions It is unlikely to have the same split at every level for a randomly ordered array. What happens if "good" splits alternate with bad splits? n Cost of 2 levels = 2n -1 = Q(n) 1 n-1 (n-1)/2 (n-1)/2 Number of levels < 2lgn (Why?) Running time = O(nlgn)

Average case running time of Quick Sort The growth of the running time for QuickSort when the levels alternate between good and bad splits is like the growth of the running time for good splits alone: O(nlgn) On average, QuickSort has a running time that is O(nlgn). This assumes that we will encounter the various inputs with equal likelihood. Instead of assuming all permutations are equally likely, we can enforce it by permuting the elements before sorting the array. Alternatively, we can choose the pivot at random from all the elements of the array.

Randomized Version of QuickSort Randomized-Quick-Sort(A) Randomized-QSort(A, 1, length[A]) Randomized-QSort(A, lo, hi) if lo < hi p = Randomized-Partition(A, lo, hi) Randomized-QSort(A, lo, p) Randomized-QSort(A, p + 1, hi) Randomized-Partition(A, lo, hi) i = random(lo, hi) // before array is partitioned swap(A, lo, i) // swap A[lo] with an element chosen return Partition(A, lo, hi) // at random from A[lo..hi]. This // makes pivot element equally // likely to be any of the subarray // elements.

Worst Case Running Time How does randomized QuickSort affect the worst case running time? (We will discuss this in class)

Analyzing Randomized QuickSort Average Case Idea: Because the algorithm chooses the pivot at random, we can estimate the probability of each size partition. Chance of a (1, n-1) split is 2/n (if pivot is smallest or next to smallest) Chance of other splits is 1/n each We can establish the following recurrence equation: It can be shown that the solution to the above equation is: T(n) = O(nlgn)

Lower bound for comparison based sorts So far in this class, the best sorting algorithms have all run in O(nlgn) time: merge-sort: Worst case running time: Q(nlgn) heap-sort: Worst case running time O(nlgn) quick-sort: Average case running time O(nlgn) Can we do better? Before we try to find a better comparison based sorting algorithm, can we put a lower bound on all comparison based algorithms?

Assumptions Assume the following: We are given an input sequence <a1, ... , an> Allowable comparisons: ai < aj, ai<=aj, ai=aj ai > aj, ai>=aj No other tools. Assume all the elements ai are distinct. (One can prove the lower bound without this, but it is more complex). We do not need to consider the strict equality, ai = aj Under the above assumption, all of the following comparisons yield identical information regarding the relative order of ai and aj: ai<=aj, ai>=aj, ai<aj, ai>aj Therefore we can assume all comparisons are of the form ai<=aj

Decision Tree for n=3 We will work out the decision tree for insertion sort, n = 3.

Analyzing the tree Worst case number of comparisons = Length of longest path from root to leaf = height of decision tree. Lower bound on height of the tree = lower bound on the running time of any comparison based algorithm. Height of tree: (We will work this out in class) Are Heap sort and Merge sort asymptotically optimal comparison sorts?