David Luebke 1 2/19/2016 Priority Queues Quicksort.

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.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
Analysis of Algorithms
ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions.
Order Statistics(Selection Problem) A more interesting problem is selection:  finding the i th smallest element of a set We will show: –A practical randomized.
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)
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Quicksort Ack: Several slides from Prof. Jim Anderson’s COMP 750 notes. UNC Chapel Hill1.
Introduction to Algorithms Chapter 7: Quick Sort.
Quicksort Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
September 19, Algorithms and Data Structures Lecture IV Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
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.
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.
David Luebke 1 7/2/2015 Merge Sort Solving Recurrences The Master Theorem.
David Luebke 1 7/2/2015 Linear-Time Sorting Algorithms.
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place.
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
David Luebke 1 8/17/2015 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
10 Algorithms in 20th Century Science, Vol. 287, No. 5454, p. 799, February 2000 Computing in Science & Engineering, January/February : The Metropolis.
David Luebke 1 9/8/2015 CS 332: Algorithms Review for Exam 1.
Efficient Algorithms Quicksort. Quicksort A common algorithm for sorting non-sorted arrays. Worst-case running time is O(n 2 ), which is actually the.
David Luebke 1 10/3/2015 CS 332: Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
MS 101: Algorithms Instructor Neelima Gupta
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.
10/13/20151 CS 3343: Analysis of Algorithms Lecture 9: Review for midterm 1 Analysis of quick sort.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
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.
David Luebke 1 6/3/2016 CS 332: Algorithms Heapsort Priority Queues Quicksort.
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.
CS 2133: Data Structures Quicksort. Review: Heaps l A heap is a “complete” binary tree, usually represented as an array:
Order Statistics(Selection Problem)
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.
David Luebke 1 12/23/2015 Heaps & Priority Queues.
CSC 413/513: Intro to Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
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/5/2016 CS 332: Algorithms Introduction to heapsort.
Mudasser Naseer 1 3/4/2016 CSC 201: Design and Analysis of Algorithms Lecture # 6 Bubblesort Quicksort.
David Luebke 1 6/26/2016 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
David Luebke 1 7/2/2016 CS 332: Algorithms Linear-Time Sorting: Review + Bucket Sort Medians and Order Statistics.
Analysis of Algorithms CS 477/677
CS 3343: Analysis of Algorithms
Quick Sort Divide: Partition the array into two sub-arrays
Introduction to Algorithms Prof. Charles E. Leiserson
Heaps, Heapsort, and Priority Queues
CSC 413/513: Intro to Algorithms
Heaps, Heapsort, and Priority Queues
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.
Solving Recurrences Continued The Master Theorem
Quicksort Quick sort Correctness of partition - loop invariant
Presentation transcript:

David Luebke 1 2/19/2016 Priority Queues Quicksort

David Luebke 2 2/19/2016 Priority Queues l Heapsort is a nice algorithm, but in practice Quicksort (coming up) usually wins l But the heap data structure is incredibly useful for implementing priority queues n A data structure for maintaining a set S of elements, each with an associated value or key Supports the operations Insert(), Maximum(), and ExtractMax() n What might a priority queue be useful for?

David Luebke 3 2/19/2016 Priority Queue Operations l Insert(S, x) inserts the element x into set S l Maximum(S) returns the element of S with the maximum key l ExtractMax(S) removes and returns the element of S with the maximum key l How could we implement these operations using a heap?

David Luebke 4 2/19/2016 Implementing Priority Queues HeapInsert(A, key) // what’s running time? { heap_size[A] ++; i = heap_size[A]; while (i > 1 AND A[Parent(i)] < key) { A[i] = A[Parent(i)]; i = Parent(i); } A[i] = key; }

David Luebke 5 2/19/2016 Implementing Priority Queues HeapMaximum(A) { // This one is really tricky: return A[i]; }

David Luebke 6 2/19/2016 Implementing Priority Queues HeapExtractMax(A) { if (heap_size[A] < 1) { error; } max = A[1]; A[1] = A[heap_size[A]] heap_size[A] --; Heapify(A, 1); return max; }

David Luebke 7 2/19/2016 Quicksort l Another divide-and-conquer algorithm n The array A[p..r] is partitioned into two non- empty subarrays A[p..q] and A[q+1..r] u Invariant: All elements in A[p..q] are less than all elements in A[q+1..r] n The subarrays are recursively sorted by calls to quicksort n Unlike merge sort, no combining step: two subarrays form an already-sorted array

David Luebke 8 2/19/2016 Quicksort Code Quicksort(A, p, r) { if (p < r) { q = Partition(A, p, r); Quicksort(A, p, q); Quicksort(A, q+1, r); }

David Luebke 9 2/19/2016 Partition Clearly, all the action takes place in the partition() function n Rearranges the subarray in place n End result: u Two subarrays u All values in first subarray  all values in second n Returns the index of the “pivot” element separating the two subarrays l How do you suppose we implement this function?

David Luebke 10 2/19/2016 Partition In Words l Partition(A, p, r): n Select an element to act as the “pivot” (which?) n Grow two regions, A[p..i] and A[j..r] u All elements in A[p..i] <= pivot u All elements in A[j..r] >= pivot n Increment i until A[i] >= pivot n Decrement j until A[j] <= pivot n Swap A[i] and A[j] n Repeat until i >= j n Return j Note: slightly different from book’s partition()

David Luebke 11 2/19/2016 Partition Code Partition(A, p, r) x = A[p]; i = p - 1; j = r + 1; while (TRUE) repeat j--; until A[j] <= x; repeat i++; until A[i] >= x; if (i < j) Swap(A, i, j); else return j; Illustrate on A = {5, 3, 2, 6, 4, 1, 3, 7}; What is the running time of partition() ?

David Luebke 12 2/19/2016 Partition Code Partition(A, p, r) x = A[p]; i = p - 1; j = r + 1; while (TRUE) repeat j--; until A[j] <= x; repeat i++; until A[i] >= x; if (i < j) Swap(A, i, j); else return j; partition() runs in O(n) time

David Luebke 13 2/19/2016 Analyzing Quicksort l What will be the worst case for the algorithm? n Partition is always unbalanced l What will be the best case for the algorithm? n Partition is perfectly balanced l Which is more likely? n The latter, by far, except... l Will any particular input elicit the worst case? n Yes: Already-sorted input

David Luebke 14 2/19/2016 Analyzing Quicksort l In the worst case: T(1) =  (1) T(n) = T(n - 1) +  (n) l Works out to T(n) =  (n 2 )

David Luebke 15 2/19/2016 Analyzing Quicksort l In the best case: T(n) = 2T(n/2) +  (n) l What does this work out to? T(n) =  (n lg n)

David Luebke 16 2/19/2016 Improving Quicksort l The real liability of quicksort is that it runs in O(n 2 ) on already-sorted input l Book discusses two solutions: n Randomize the input array, OR n Pick a random pivot element l How will these solve the problem? n By insuring that no particular input can be chosen to make quicksort run in O(n 2 ) time

David Luebke 17 2/19/2016 Analyzing Quicksort: Average Case l Assuming random input, average-case running time is much closer to O(n lg n) than O(n 2 ) l First, a more intuitive explanation/example: n Suppose that partition() always produces a 9-to-1 split. This looks quite unbalanced! n The recurrence is thus: T(n) = T(9n/10) + T(n/10) + n n How deep will the recursion go? (draw it) Use n instead of O(n) for convenience (how?)

David Luebke 18 2/19/2016 Analyzing Quicksort: Average Case l Intuitively, a real-life run of quicksort will produce a mix of “bad” and “good” splits n Randomly distributed among the recursion tree n Pretend for intuition that they alternate between best-case (n/2 : n/2) and worst-case (n-1 : 1) n What happens if we bad-split root node, then good-split the resulting size (n-1) node?

David Luebke 19 2/19/2016 Analyzing Quicksort: Average Case l Intuitively, a real-life run of quicksort will produce a mix of “bad” and “good” splits n Randomly distributed among the recursion tree n Pretend for intuition that they alternate between best-case (n/2 : n/2) and worst-case (n-1 : 1) n What happens if we bad-split root node, then good-split the resulting size (n-1) node? u We fail English

David Luebke 20 2/19/2016 Analyzing Quicksort: Average Case l Intuitively, a real-life run of quicksort will produce a mix of “bad” and “good” splits n Randomly distributed among the recursion tree n Pretend for intuition that they alternate between best-case (n/2 : n/2) and worst-case (n-1 : 1) n What happens if we bad-split root node, then good-split the resulting size (n-1) node? u We end up with three subarrays, size 1, (n-1)/2, (n-1)/2 u Combined cost of splits = n + n -1 = 2n -1 = O(n) u No worse than if we had good-split the root node!

David Luebke 21 2/19/2016 Analyzing Quicksort: Average Case l Intuitively, the O(n) cost of a bad split (or 2 or 3 bad splits) can be absorbed into the O(n) cost of each good split l Thus running time of alternating bad and good splits is still O(n lg n), with slightly higher constants l How can we be more rigorous?

David Luebke 22 2/19/2016 Analyzing Quicksort: Average Case l For simplicity, assume: n All inputs distinct (no repeats) Slightly different partition() procedure u partition around a random element, which is not included in subarrays u all splits (0:n-1, 1:n-2, 2:n-3, …, n-1:0) equally likely l What is the probability of a particular split happening? l Answer: 1/n

David Luebke 23 2/19/2016 Analyzing Quicksort: Average Case l So partition generates splits (0:n-1, 1:n-2, 2:n-3, …, n-2:1, n-1:0) each with probability 1/n l If T(n) is the expected running time, l What is each term under the summation for? l What is the  (n) term for?

David Luebke 24 2/19/2016 Analyzing Quicksort: Average Case l So… n Note: this is just like the book’s recurrence (p166), except that the summation starts with k=0 n We’ll take care of that in a second Write it on the board

David Luebke 25 2/19/2016 Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n Guess the answer n Assume that the inductive hypothesis holds n Substitute it in for some value < n n Prove that it follows for n

David Luebke 26 2/19/2016 Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n Guess the answer u What’s the answer? n Assume that the inductive hypothesis holds n Substitute it in for some value < n n Prove that it follows for n

David Luebke 27 2/19/2016 Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n Guess the answer u T(n) = O(n lg n) n Assume that the inductive hypothesis holds n Substitute it in for some value < n n Prove that it follows for n

David Luebke 28 2/19/2016 Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n Guess the answer u T(n) = O(n lg n) n Assume that the inductive hypothesis holds u What’s the inductive hypothesis? n Substitute it in for some value < n n Prove that it follows for n

David Luebke 29 2/19/2016 Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n Guess the answer u T(n) = O(n lg n) n Assume that the inductive hypothesis holds u T(n)  an lg n + b for some constants a and b n Substitute it in for some value < n n Prove that it follows for n

David Luebke 30 2/19/2016 Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n Guess the answer u T(n) = O(n lg n) n Assume that the inductive hypothesis holds u T(n)  an lg n + b for some constants a and b n Substitute it in for some value < n u What value? n Prove that it follows for n

David Luebke 31 2/19/2016 Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n Guess the answer u T(n) = O(n lg n) n Assume that the inductive hypothesis holds u T(n)  an lg n + b for some constants a and b n Substitute it in for some value < n u The value k in the recurrence n Prove that it follows for n

David Luebke 32 2/19/2016 Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n Guess the answer u T(n) = O(n lg n) n Assume that the inductive hypothesis holds u T(n)  an lg n + b for some constants a and b n Substitute it in for some value < n u The value k in the recurrence n Prove that it follows for n u Grind through it…

David Luebke 33 2/19/2016 Note: leaving the same recurrence as the book What are we doing here? Analyzing Quicksort: Average Case The recurrence to be solved What are we doing here? Plug in inductive hypothesis Expand out the k=0 case 2b/n is just a constant, so fold it into  (n)

David Luebke 34 2/19/2016 What are we doing here? Evaluate the summation: b+b+…+b = b (n-1) The recurrence to be solved Since n-1<n, 2b(n-1)/n < 2b Analyzing Quicksort: Average Case What are we doing here? Distribute the summation This summation gets its own set of slides later

David Luebke 35 2/19/2016 How did we do this? Pick a large enough that an/4 dominates  (n)+b What are we doing here? Remember, our goal is to get T(n)  an lg n + b What the hell? We’ll prove this later What are we doing here? Distribute the (2a/n) term The recurrence to be solved Analyzing Quicksort: Average Case

David Luebke 36 2/19/2016 Analyzing Quicksort: Average Case l So T(n)  an lg n + b for certain a and b n Thus the induction holds n Thus T(n) = O(n lg n) n Thus quicksort runs in O(n lg n) time on average (phew!) l Oh yeah, the summation…

David Luebke 37 2/19/2016 What are we doing here? The lg k in the second term is bounded by lg n Tightly Bounding The Key Summation What are we doing here? Move the lg n outside the summation What are we doing here? Split the summation for a tighter bound

David Luebke 38 2/19/2016 The summation bound so far Tightly Bounding The Key Summation What are we doing here? The lg k in the first term is bounded by lg n/2 What are we doing here? lg n/2 = lg n - 1 What are we doing here? Move (lg n - 1) outside the summation

David Luebke 39 2/19/2016 The summation bound so far Tightly Bounding The Key Summation What are we doing here? Distribute the (lg n - 1) What are we doing here? The summations overlap in range; combine them What are we doing here? The Guassian series

David Luebke 40 2/19/2016 The summation bound so far Tightly Bounding The Key Summation What are we doing here? Rearrange first term, place upper bound on second What are we doing? X Guassian series What are we doing? Multiply it all out

David Luebke 41 2/19/2016 Tightly Bounding The Key Summation