Lower Bounds for Comparison-Based Sorting Algorithms (Ch. 8)

Slides:



Advertisements
Similar presentations
Sorting in linear time (for students to read) Comparison sort: –Lower bound:  (nlgn). Non comparison sort: –Bucket sort, counting sort, radix sort –They.
Advertisements

Analysis of Algorithms CS 477/677 Linear Sorting Instructor: George Bebis ( Chapter 8 )
Sorting in Linear Time Comp 550, Spring Linear-time Sorting Depends on a key assumption: numbers to be sorted are integers in {0, 1, 2, …, k}. Input:
Non-Comparison Based Sorting
CSE 3101: Introduction to the Design and Analysis of Algorithms
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
MS 101: Algorithms Instructor Neelima Gupta
1 Sorting in Linear Time How can we do better?  CountingSort  RadixSort  BucketSort.
Sorting in linear time Comparison sort: –Lower bound:  (nlgn). Non comparison sort: –Bucket sort, counting sort, radix sort –They are possible in linear.
Mudasser Naseer 1 5/1/2015 CSC 201: Design and Analysis of Algorithms Lecture # 9 Linear-Time Sorting Continued.
Counting Sort Non-comparison sort. Precondition: n numbers in the range 1..k. Key ideas: For each x count the number C(x) of elements ≤ x Insert x at output.
Lower bound for sorting, radix sort COMP171 Fall 2005.
CSCE 3110 Data Structures & Algorithm Analysis
Using Divide and Conquer for Sorting
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.
Lower bound for sorting, radix sort COMP171 Fall 2006.
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.
CS 253: Algorithms Chapter 8 Sorting in Linear Time Credit: Dr. George Bebis.
Comp 122, Spring 2004 Keys into Buckets: Lower bounds, Linear-time sort, & Hashing.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Comp 122, Spring 2004 Lower Bounds & Sorting in Linear Time.
Analysis of Algorithms CS 477/677 Midterm Exam Review Instructor: George Bebis.
Lecture 5: Master Theorem and Linear Time Sorting
Analysis of Algorithms CS 477/677
1 Today’s Material Lower Bounds on Comparison-based Sorting Linear-Time Sorting Algorithms –Counting Sort –Radix Sort.
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.
Sorting Lower Bound1. 2 Comparison-Based Sorting (§ 4.4) Many sorting algorithms are comparison based. They sort by making comparisons between pairs of.
Computer Algorithms Lecture 11 Sorting in Linear Time Ch. 8
Data Structure & Algorithm Lecture 7 – Linear Sort JJCAO Most materials are stolen from Prof. Yoram Moses’s course.
Sorting in Linear Time Lower bound for comparison-based sorting
CSE 373 Data Structures Lecture 15
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
1 Sorting in O(N) time CS302 Data Structures Section 10.4.
David Luebke 1 10/13/2015 CS 332: Algorithms Linear-Time Sorting Algorithms.
CSC 41/513: Intro to Algorithms Linear-Time Sorting Algorithms.
Sorting Fun1 Chapter 4: Sorting     29  9.
Analysis of Algorithms CS 477/677
Fall 2015 Lecture 4: Sorting in linear time
Mudasser Naseer 1 11/5/2015 CSC 201: Design and Analysis of Algorithms Lecture # 8 Some Examples of Recursion Linear-Time Sorting Algorithms.
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.
Analysis of Algorithms CS 477/677 Lecture 8 Instructor: Monica Nicolescu.
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
Linear Sorting. Comparison based sorting Any sorting algorithm which is based on comparing the input elements has a lower bound of Proof, since there.
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.
Lecture 5 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
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 7/2/2016 CS 332: Algorithms Linear-Time Sorting: Review + Bucket Sort Medians and Order Statistics.
Lower Bounds & Sorting in Linear Time
Sorting.
Introduction to Algorithms
Algorithm Design and Analysis (ADA)
Lecture 5 Algorithm Analysis
Linear Sorting Sections 10.4
Keys into Buckets: Lower bounds, Linear-time sort, & Hashing
Ch8: Sorting in Linear Time Ming-Te Chi
Lecture 5 Algorithm Analysis
Sorting in linear time (for students to read)
Linear Sort "Our intuition about the future is linear. But the reality of information technology is exponential, and that makes a profound difference.
Linear Sorting Sorting in O(n) Jeff Chastine.
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 Sorting Section 10.4
Linear-Time Sorting Algorithms
Lecture 5 Algorithm Analysis
Lower bound for sorting, radix sort
Algorithms CSCI 235, Spring 2019 Lecture 18 Linear Sorting
CS 583 Analysis of Algorithms
Lecture 5 Algorithm Analysis
Presentation transcript:

Lower Bounds for Comparison-Based Sorting Algorithms (Ch. 8) We have seen several sorting algorithms that run in (nlgn) time in the worst case (meaning there is some input on which the algorithm execution takes at least (nlgn) time). mergesort heapsort quicksort In all comparison-based sorting algorithms, the sorted order results only from comparisons between input elements. Is it possible for any comparison-based sorting algorithm to do better?

Lower Bounds for Sorting Algorithms Theorem: Any comparison-based sort must make (nlgn) comparisons in the worst case to sort a sequence of n elements. But how do we prove this? We'll use the decision tree model to represent any sorting algorithm and then argue that no matter the algorithm, there is some input that will cause it to run in (nlgn) time.

The Decision Tree Model Given any comparison-based sorting algorithm, we can represent its behavior on an input of size n by a decision tree. Note: we need only consider the comparisons in the algorithm (the other operations only make the algorithm take longer). each internal node in the decision tree corresponds to one of the comparisons in the algorithm. start at the root and do first comparison: - if  take left branch, if > take right branch, etc. each leaf represents one possible ordering of the input - one leaf for each of n! possible orderings.  One decision tree exists for each algorithm and input size

The Decision Tree Model Example: Insertion sort with n = 3 (3! = 6 leaves)  A[1] vs A[2] > A[2] vs A[3] A[1] vs A[3]  > >  A[1] vs A[3] A[1], A[2], A[3] A[2] vs A[3] A[2], A[1], A[3]  >  > A[1], A[3], A[2] A[3], A[1], A[2] A[2], A[3], A[1] A[3], A[2], A[1] Note: The length of the longest root to leaf path in this tree = worst-case number of comparisons  worst-case number of operations of algorithm

The (nlgn) Lower Bound Theorem: Any decision tree T for sorting n elements has height (nlgn) (therefore, any comparison sort algorithm requires (nlgn) compares in the worst case). Proof: Let h be the height of T. Then we know T has at least n! leaves T is binary, so it has at most 2h leaves n!  #leaves  2h 2h  n! lg(2h)  lg(n!) h  (nlgn) (Eq. 3.18) 

Beating the lower bound ... non-comparison based sorts Idea: Algorithms that are NOT comparison based might be faster. There are three such algorithms presented in Chapter 8: counting sort radix sort bucket sort These algorithms run in O(n) time (if certain conditions can be guaranteed) either use information about the values to be sorted (counting sort, bucket sort), or operate on "pieces" of the input elements (radix sort)

Counting Sort Requirement: input elements are integers in known range [0..k] for some constant k. Idea: for each input element x, find the number of elements  x (say this number = m) and put x in the (m+1)st spot in the output array. Counting-Sort(A, k) // A[1..n] is input array, C[0..k] is initially all 0's, B[1..n] is output array 1. for i  1 to length(A) do 2. C[A[i]]  C[A[i]] + 1 3. for i  1 to k do 4. C[i]  C[i] + C[i-1] // Make C into a "prefix sum" array, where C[i] // contains number of elements  i 5. for j  length(A) downto 1 do 6. B[C[A[j]]]  A[j] 7. C[A[j]]  C[A[j]] - 1

Running Time of Counting Sort for loop in lines 1-2 takes (n) time. for loop in lines 3-4 takes (k) time. for loop in lines 5-7 takes (n) time. In practice, use counting sort when we have k = (n), so running time is (n). Counting sort has the important property of stability: A sorting algorithm is stable when numbers with the same values appear in the output array in the same order as they do in the input array. Important when satellite data is stored with elements being sorted and when counting sort is used as a subroutine for radix sort. Overall time is (k + n).

Radix Sort Let d be the number of digits in each input number. Radix-Sort(A, d) 1. for i  1 to d do 2. use stable sort to sort array A on digit i Note: radix sort sorts the least significant digit first. correctness can be shown by induction on the digit being sorted. often counting sort is used as the internal sort in step 2.

Radix Sort Let d be the number of digits in each input number. Radix-Sort(A, d) 1. for i  1 to d do 2. use stable sort to sort array A on digit i Running time of radix sort: O(dTss(n)) Tss is the time for the internal sort. Counting sort gives Tss(n) = O(k + n), so O(dTss(n)) = O(d(k + n)), which is O(n) if d = O(1) and k = O(n). if d = O(lgn) and k = 2 (common for computers), then O(d(k+n)) = O(nlgn)

Bucket Sort Assumption: input elements distributed uniformly over some known range, e.g., [0,1), so all elements in A are greater than or equal to 0 but less than 1 . (Appendix C.2 has defn of uniform distribution) Bucket-Sort(A) 1. n = length[A] 2. for i = 1 to n do insert A[i] into list B[floor of nA[i]] 4. for i = 0 to n-1 do sort list i with Insertion-Sort 6. Concatenate lists B[0], B[1],…,B[n-1]

Bucket Sort Bucket-Sort(A, x, y) 1. divide interval [x, y) into n equal-sized subintervals (buckets) 2. distribute the n input keys into the buckets 3. sort the numbers in each bucket (e.g., with insertion sort) 4. scan the (sorted) buckets in order and produce output array Running time of bucket sort: O(n) expected time Step 1: O(1) for each interval = O(n) time total. Step 2: O(n) time. Step 3: The expected number of elements in each bucket is O(1) (see book for formal argument, section 8.4), so total is O(n) Step 4: O(n) time to scan the n buckets containing a total of n input elements

Summary NCB Sorts Non-Comparison Based Sorts Running Time worst-case average-case best-case in place Counting Sort O(n + k) O(n + k) O(n + k) no Radix Sort O(d(n + k')) O(d(n + k')) O(d(n + k')) no Bucket Sort O(n) no Counting sort assumes input elements are in range [0,1,2,..,k] and uses array indexing to count the number of occurrences of each value. Radix sort assumes each integer consists of d digits, and each digit is in range [1,2,..,k']. Bucket sort requires advance knowledge of input distribution (sorts n numbers uniformly distributed in range in O(n) time).