David Luebke 1 7/2/2016 CS 332: Algorithms Linear-Time Sorting: Review + Bucket Sort Medians and Order Statistics.

Slides:



Advertisements
Similar presentations
©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 6 Prof. Erik Demaine.
Advertisements

Comp 122, Spring 2004 Order Statistics. order - 2 Lin / Devi Comp 122 Order Statistic i th order statistic: i th smallest element of a set of n elements.
David Luebke 1 6/1/2014 CS 332: Algorithms Medians and Order Statistics Structures for Dynamic Sets.
David Luebke 1 4/22/2015 CS 332: Algorithms Quicksort.
Analysis of Algorithms CS 477/677 Linear Sorting Instructor: George Bebis ( Chapter 8 )
CSE 3101: Introduction to the Design and Analysis of Algorithms
MS 101: Algorithms Instructor Neelima Gupta
Mudasser Naseer 1 5/1/2015 CSC 201: Design and Analysis of Algorithms Lecture # 9 Linear-Time Sorting Continued.
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.
CS 3343: Analysis of Algorithms Lecture 14: Order Statistics.
1 Selection --Medians and Order Statistics (Chap. 9) The ith order statistic of n elements S={a 1, a 2,…, a n } : ith smallest elements Also called selection.
Introduction to Algorithms
Introduction to Algorithms Jiafen Liu Sept
CSCE 3110 Data Structures & Algorithm Analysis
Spring 2015 Lecture 5: QuickSort & Selection
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.
Comp 122, Spring 2004 Lower Bounds & Sorting in Linear Time.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Median, order statistics. Problem Find the i-th smallest of n elements.  i=1: minimum  i=n: maximum  i= or i= : median Sol: sort and index the i-th.
Analysis of Algorithms CS 477/677
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 5 Linear-time sorting Can we do better than comparison sorting? Linear-time sorting.
David Luebke 1 7/2/2015 Linear-Time Sorting Algorithms.
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
Lower Bounds for Comparison-Based Sorting Algorithms (Ch. 8)
David Luebke 1 8/17/2015 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
Computer Algorithms Lecture 11 Sorting in Linear Time Ch. 8
Sorting in Linear Time Lower bound for comparison-based sorting
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
David Luebke 1 9/8/2015 CS 332: Algorithms Review for Exam 1.
1 Sorting in O(N) time CS302 Data Structures Section 10.4.
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.
David Luebke 1 10/13/2015 CS 332: Algorithms Linear-Time Sorting Algorithms.
CSC 41/513: Intro to Algorithms Linear-Time Sorting Algorithms.
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
Introduction to Algorithms Jiafen Liu Sept
David Luebke 1 6/3/2016 CS 332: Algorithms Analyzing Quicksort: Average Case.
Chapter 9: Selection Order Statistics What are an order statistic? min, max median, i th smallest, etc. Selection means finding a particular order statistic.
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.
Mudasser Naseer 1 11/5/2015 CSC 201: Design and Analysis of Algorithms Lecture # 8 Some Examples of Recursion Linear-Time Sorting Algorithms.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
Order Statistics(Selection Problem)
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
1 Medians and Order Statistics CLRS Chapter 9. upper median lower median The lower median is the -th order statistic The upper median.
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,
1 Algorithms CSCI 235, Fall 2015 Lecture 17 Linear Sorting.
Mudasser Naseer 1 1/25/2016 CS 332: Algorithms Lecture # 10 Medians and Order Statistics Structures for Dynamic Sets.
David Luebke 1 2/19/2016 Priority Queues Quicksort.
Linear Sorting. Comparison based sorting Any sorting algorithm which is based on comparing the input elements has a lower bound of Proof, since there.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting So Far Insertion sort: –Easy to code –Fast on small inputs (less than ~50 elements) –Fast on nearly-sorted.
David Luebke 1 6/26/2016 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
Chapter 9: Selection of Order Statistics What are an order statistic? min, max median, i th smallest, etc. Selection means finding a particular order statistic.
Lower Bounds & Sorting in Linear Time
Many slides here are based on E. Demaine , D. Luebke slides
Linear-Time Sorting Continued Medians and Order Statistics
Randomized Algorithms
MCA 301: Design and Analysis of Algorithms
Introduction to Algorithms
Order Statistics(Selection Problem)
Randomized Algorithms
Linear Sort "Our intuition about the future is linear. But the reality of information technology is exponential, and that makes a profound difference.
Medians and Order Statistics
CS 3343: Analysis of Algorithms
Linear Sort "Our intuition about the future is linear. But the reality of information technology is exponential, and that makes a profound difference.
Lower Bounds & Sorting in Linear Time
Linear-Time Sorting Algorithms
CS 583 Analysis of Algorithms
The Selection Problem.
CS200: Algorithm Analysis
Presentation transcript:

David Luebke 1 7/2/2016 CS 332: Algorithms Linear-Time Sorting: Review + Bucket Sort Medians and Order Statistics

David Luebke 2 7/2/2016 Review: Linear-Time Sorting l Comparison sorts: O(n lg n) at best n Model sort with decision tree n Path down tree = execution trace of algorithm n Leaves of tree = possible permutations of input n Tree must have n! leaves, so O(n lg n) height

David Luebke 3 7/2/2016 Review: Counting Sort l Counting sort: n Assumption: input is in the range 1..k n Basic idea: u Count number of elements k  each element i u Use that number to place i in position k of sorted array n No comparisons! Runs in time O(n + k) n Stable sort n Does not sort in place: u O(n) array to hold sorted output u O(k) array for scratch storage

David Luebke 4 7/2/2016 Review: Counting Sort 1CountingSort(A, B, k) 2for i=1 to k 3C[i]= 0; 4for j=1 to n 5C[A[j]] += 1; 6for i=2 to k 7C[i] = C[i] + C[i-1]; 8for j=n downto 1 9B[C[A[j]]] = A[j]; 10C[A[j]] -= 1;

David Luebke 5 7/2/2016 Review: Radix Sort l Radix sort: n Assumption: input has d digits ranging from 0 to k n Basic idea: u Sort elements by digit starting with least significant u Use a stable sort (like counting sort) for each stage n Each pass over n numbers with d digits takes time O(n+k), so total time O(dn+dk) u When d is constant and k=O(n), takes O(n) time n Fast! Stable! Simple! n Doesn’t sort in place

David Luebke 6 7/2/2016 Bucket Sort l Bucket sort n Assumption: input is n reals from [0, 1) n Basic idea: u Create n linked lists (buckets) to divide interval [0,1) into subintervals of size 1/n u Add each input element to appropriate bucket and sort buckets with insertion sort n Uniform input distribution  O(1) bucket size u Therefore the expected total time is O(n) n These ideas will return when we study hash tables

David Luebke 7 7/2/2016 Order Statistics l The ith order statistic in a set of n elements is the ith smallest element l The minimum is thus the 1st order statistic l The maximum is (duh) the nth order statistic l The median is the n/2 order statistic n If n is even, there are 2 medians l How can we calculate order statistics? l What is the running time?

David Luebke 8 7/2/2016 Order Statistics l How many comparisons are needed to find the minimum element in a set? The maximum? l Can we find the minimum and maximum with less than twice the cost? l Yes: n Walk through elements by pairs u Compare each element in pair to the other u Compare the largest to maximum, smallest to minimum n Total cost: 3 comparisons per 2 elements = O(3n/2)

David Luebke 9 7/2/2016 Finding Order Statistics: The Selection Problem l A more interesting problem is selection: finding the ith smallest element of a set l We will show: n A practical randomized algorithm with O(n) expected running time n A cool algorithm of theoretical interest only with O(n) worst-case running time

David Luebke 10 7/2/2016 Randomized Selection l Key idea: use partition() from quicksort n But, only need to examine one subarray n This savings shows up in running time: O(n) l We will again use a slightly different partition than the book: q = RandomizedPartition(A, p, r)  A[q]  A[q] q pr

David Luebke 11 7/2/2016 Randomized Selection RandomizedSelect(A, p, r, i) if (p == r) then return A[p]; q = RandomizedPartition(A, p, r) k = q - p + 1; if (i == k) then return A[q]; // not in book if (i < k) then return RandomizedSelect(A, p, q-1, i); else return RandomizedSelect(A, q+1, r, i-k);  A[q]  A[q] k q pr

David Luebke 12 7/2/2016 Randomized Selection Analyzing RandomizedSelect() n Worst case: partition always 0:n-1 T(n) = T(n-1) + O(n)= ??? = O(n 2 ) (arithmetic series) u Worse than sorting! n “Best” case: suppose a 9:1 partition T(n) = T(9n/10) + O(n) = ??? = O(n)(Master Theorem, case 3) u Better than sorting! u What if this had been a 99:1 split?

David Luebke 13 7/2/2016 Randomized Selection l Average case n For upper bound, assume ith element always falls in larger side of partition: n Let’s show that T(n) = O(n) by substitution What happened here?

David Luebke 14 7/2/2016 What happened here?“Split” the recurrence What happened here? Randomized Selection l Assume T(n)  cn for sufficiently large c: The recurrence we started with Substitute T(n)  cn for T(k) Expand arithmetic series Multiply it out

David Luebke 15 7/2/2016 What happened here?Subtract c/2 What happened here? Randomized Selection l Assume T(n)  cn for sufficiently large c: The recurrence so far Multiply it out Rearrange the arithmetic What we set out to prove

David Luebke 16 7/2/2016 Worst-Case Linear-Time Selection l Randomized algorithm works well in practice l What follows is a worst-case linear time algorithm, really of theoretical interest only l Basic idea: n Generate a good partitioning element n Call this element x

David Luebke 17 7/2/2016 Worst-Case Linear-Time Selection l The algorithm in words: 1.Divide n elements into groups of 5 2.Find median of each group (How? How long?) 3.Use Select() recursively to find median x of the  n/5  medians 4.Partition the n elements around x. Let k = rank(x) 5.if (i == k) then return x if (i k) use Select() recursively to find (i-k)th smallest element in last partition

David Luebke 18 7/2/2016 Worst-Case Linear-Time Selection l (Sketch situation on the board) l How many of the 5-element medians are  x? n At least 1/2 of the medians =   n/5  / 2  =  n/10  l How many elements are  x? n At least 3  n/10  elements l For large n, 3  n/10   n/4 (How large?) l So at least n/4 elements  x l Similarly: at least n/4 elements  x

David Luebke 19 7/2/2016 Worst-Case Linear-Time Selection l Thus after partitioning around x, step 5 will call Select() on at most 3n/4 elements l The recurrence is therefore: ???  n/5   n/5 Substitute T(n) = cn Combine fractions Express in desired form What we set out to prove

David Luebke 20 7/2/2016 Worst-Case Linear-Time Selection l Intuitively: n Work at each level is a constant fraction (19/20) smaller u Geometric progression! n Thus the O(n) work at the root dominates

David Luebke 21 7/2/2016 Linear-Time Median Selection l Given a “black box” O(n) median algorithm, what can we do? n ith order statistic: u Find median x u Partition input around x u if (i  (n+1)/2) recursively find ith element of first half u else find (i - (n+1)/2)th element in second half u T(n) = T(n/2) + O(n) = O(n) n Can you think of an application to sorting?

David Luebke 22 7/2/2016 Linear-Time Median Selection l Worst-case O(n lg n) quicksort n Find median x and partition around it n Recursively quicksort two halves n T(n) = 2T(n/2) + O(n) = O(n lg n)