Lower bound for sorting, radix sort COMP171 Fall 2006.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
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
Mudasser Naseer 1 5/1/2015 CSC 201: Design and Analysis of Algorithms Lecture # 9 Linear-Time Sorting Continued.
CSE332: Data Abstractions Lecture 14: Beyond Comparison Sorting Dan Grossman Spring 2010.
Lower bound for sorting, radix sort COMP171 Fall 2005.
© 2004 Goodrich, Tamassia Sorting Lower Bound1. © 2004 Goodrich, Tamassia Sorting Lower Bound2 Comparison-Based Sorting (§ 10.3) Many sorting algorithms.
CSE332: Data Abstractions Lecture 14: Beyond Comparison Sorting Tyler Robison Summer
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.
Comp 122, Spring 2004 Lower Bounds & Sorting in Linear Time.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
© 2004 Goodrich, Tamassia Sorting Lower Bound1. © 2004 Goodrich, Tamassia Sorting Lower Bound2 Comparison-Based Sorting (§ 10.3) Many sorting algorithms.
Lecture 5: Master Theorem and Linear Time Sorting
TTIT33 Algorithms and Optimization – Dalg Lecture 2 HT TTIT33 Algorithms and optimization Lecture 2 Algorithms Sorting [GT] 3.1.2, 11 [LD] ,
CSE 326: Data Structures Sorting Ben Lerner Summer 2007.
Sorting Lower Bound Andreas Klappenecker based on slides by Prof. Welch 1.
Analysis of Algorithms CS 477/677
1 Today’s Material Lower Bounds on Comparison-based Sorting Linear-Time Sorting Algorithms –Counting Sort –Radix Sort.
Lecture 8 Sorting. Sorting (Chapter 7) We have a list of real numbers. Need to sort the real numbers in increasing order (smallest first). Important points.
HEAPSORT COUNTING SORT RADIX SORT. HEAPSORT O(nlgn) worst case like Merge sort. Like Insertion Sort, but unlike Merge Sort, Heapsort sorts in place: Combines.
Lecture 8 Sorting. Sorting (Chapter 7) We have a list of real numbers. Need to sort the real numbers in increasing order (smallest first). Important points.
Sorting Lower Bound1. 2 Comparison-Based Sorting (§ 4.4) Many sorting algorithms are comparison based. They sort by making comparisons between pairs of.
Lower Bounds for Comparison-Based Sorting Algorithms (Ch. 8)
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
David Luebke 1 10/13/2015 CS 332: Algorithms Linear-Time Sorting Algorithms.
CSC 41/513: Intro to Algorithms Linear-Time Sorting Algorithms.
CSE373: Data Structures & Algorithms Lecture 20: Beyond Comparison Sorting Dan Grossman Fall 2013.
Sorting Fun1 Chapter 4: Sorting     29  9.
CSE332: Data Abstractions Lecture 14: Beyond Comparison Sorting Dan Grossman Spring 2012.
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.
Data Structures Haim Kaplan & Uri Zwick December 2013 Sorting 1.
Linear Sorting. Comparison based sorting Any sorting algorithm which is based on comparing the input elements has a lower bound of Proof, since there.
19 March More on Sorting CSE 2011 Winter 2011.
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.
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.
David Kauchak cs062 Spring 2010
Introduction to Algorithms
(2,4) Trees 11/15/2018 9:25 AM Sorting Lower Bound Sorting Lower Bound.
Lecture 5 Algorithm Analysis
Keys into Buckets: Lower bounds, Linear-time sort, & Hashing
Ch8: Sorting in Linear Time Ming-Te Chi
Lecture 5 Algorithm Analysis
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.
Lower Bounds & Sorting in Linear Time
Linear Sorting Section 10.4
Lecture 5 Algorithm Analysis
(2,4) Trees 2/28/2019 3:21 AM Sorting Lower Bound Sorting Lower Bound.
Lower bound for sorting, radix sort
Chapter 8: Overview Comparison sorts: algorithms that sort sequences by comparing the value of elements Prove that the number of comparison required to.
David Kauchak cs302 Spring 2012
Linear Time Sorting.
Sorting We have actually seen already two efficient ways to sort:
Lecture 5 Algorithm Analysis
Presentation transcript:

Lower bound for sorting, radix sort COMP171 Fall 2006

Sorting IV / Slide 2 Lower Bound for Sorting * Mergesort and heapsort n worst-case running time is O(N log N) * Are there better algorithms? * Goal: Prove that any sorting algorithm based on only comparisons takes  (N log N) comparisons in the worst case (worse-case input) to sort N elements.

Sorting IV / Slide 3 Lower Bound for Sorting * Suppose we want to sort N distinct elements * How many possible orderings do we have for N elements? * We can have N! possible orderings (e.g., the sorted output for a,b,c can be a b c, b a c, a c b, c a b, c b a, b c a.)

Sorting IV / Slide 4 Lower Bound for Sorting * Any comparison-based sorting process can be represented as a binary decision tree. n Each node represents a set of possible orderings, consistent with all the comparisons that have been made n The tree edges are results of the comparisons

Sorting IV / Slide 5 Decision tree for Algorithm X for sorting three elements a, b, c

Sorting IV / Slide 6 Lower Bound for Sorting * A different algorithm would have a different decision tree * Decision tree for Insertion Sort on 3 elements: There exists an input ordering that corresponds to each root-to-leaf path to arrive at a sorted order. For decision tree of insertion sort, the longest path is O(N 2 ).

Sorting IV / Slide 7 Lower Bound for Sorting * The worst-case number of comparisons used by the sorting algorithm is equal to the depth of the deepest leaf n The average number of comparisons used is equal to the average depth of the leaves * A decision tree to sort N elements must have N! leaves n a binary tree of depth d has at most 2 d leaves  a binary tree with 2 d leaves must have depth at least d  the decision tree with N! leaves must have depth at least  log 2 (N!)  * Therefore, any sorting algorithm based on only comparisons between elements requires at least  log 2 (N!)  comparisons in the worst case.

Sorting IV / Slide 8 Lower Bound for Sorting * Any sorting algorithm based on comparisons between elements requires  (N log N) comparisons.

Sorting IV / Slide 9 Linear time sorting * Can we do better (linear time algorithm) if the input has special structure (e.g., uniformly distributed, every number can be represented by d digits)? Yes. * Counting sort, radix sort

Sorting IV / Slide 10 Counting Sort * Assume N integers are to be sorted, each is in the range 1 to M. * Define an array B[1..M], initialize all to 0  O(M) * Scan through the input list A[i], insert A[i] into B[A[i]]  O(N) * Scan B once, read out the nonzero integers  O(M) Total time: O(M + N) n if M is O(N), then total time is O(N) n Can be bad if range is very big, e.g. M=O(N 2 ) N=7, M = 9, Want to sort Output:

Sorting IV / Slide 11 Counting sort * What if we have duplicates? * B is an array of pointers. * Each position in the array has 2 pointers: head and tail. Tail points to the end of a linked list, and head points to the beginning. * A[j] is inserted at the end of the list B[A[j]] * Again, Array B is sequentially traversed and each nonempty list is printed out. * Time: O(M + N)

Sorting IV / Slide 12 M = 9, Wish to sort Output: Counting sort

Sorting IV / Slide 13 Radix Sort * Extra information: every integer can be represented by at most k digits n d 1 d 2 …d k where d i are digits in base r n d 1 : most significant digit n d k : least significant digit

Sorting IV / Slide 14 Radix Sort * Algorithm n sort by the least significant digit first (counting sort) => Numbers with the same digit go to same bin n reorder all the numbers: the numbers in bin 0 precede the numbers in bin 1, which precede the numbers in bin 2, and so on n sort by the next least significant digit n continue this process until the numbers have been sorted on all k digits

Sorting IV / Slide 15 Radix Sort * Least-significant-digit-first Example: 275, 087, 426, 061, 509, 170, 677,

Sorting IV / Slide

Sorting IV / Slide 17 Radix Sort * Does it work? * Clearly, if the most significant digit of a and b are different and a < b, then finally a comes before b * If the most significant digit of a and b are the same, and the second most significant digit of b is less than that of a, then b comes before a.

Sorting IV / Slide 18 Radix Sort Example 2: sorting cards n 2 digits for each card: d 1 d 2 n d 1 =  : base 4        n d 2 = A, 2, 3,...J, Q, K: base 13  A  2  3 ...  J  Q  K n  2   2  5   K

Sorting IV / Slide 19 // base 10 // d times of counting sort // re-order back to original array // scan A[i], put into correct slot // FIFO A=input array, n=|numbers to be sorted|, d=# of digits, k=the digit being sorted, j=array index Note: 171 mod 100 = div (100/10) =7 (div returns the whole # part)

Sorting IV / Slide 20 Radix Sort * Increasing the base r decreases the number of passes * Running time n k passes over the numbers (i.e. k counting sorts, with range being 0..r) n each pass takes 2N n total: O(2Nk)=O(Nk) n r and k are constants: O(N) * Note: n radix sort is not based on comparisons; the values are used as array indices n If all N input values are distinct, then k =  (log N) (e.g., in binary digits, to represent 8 different numbers, we need at least 3 digits). Thus the running time of Radix Sort also become  (N log N).