1 Today’s Material Lower Bounds on Comparison-based Sorting Linear-Time Sorting Algorithms –Counting Sort –Radix Sort.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms CS 477/677 Linear Sorting Instructor: George Bebis ( Chapter 8 )
Advertisements

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
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
CSE332: Data Abstractions Lecture 14: Beyond Comparison Sorting Dan Grossman Spring 2010.
Lower bound for sorting, radix sort COMP171 Fall 2005.
CSCE 3110 Data Structures & Algorithm Analysis
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.
© 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
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.
Sorting Heapsort Quick review of basic sorting methods Lower bounds for comparison-based methods Non-comparison based sorting.
Comp 122, Spring 2004 Lower Bounds & Sorting in Linear Time.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
2 -1 Analysis of algorithms Best case: easiest Worst case Average case: hardest.
© 2004 Goodrich, Tamassia Sorting Lower Bound1. © 2004 Goodrich, Tamassia Sorting Lower Bound2 Comparison-Based Sorting (§ 10.3) Many sorting algorithms.
CPSC 411, Fall 2008: Set 2 1 CPSC 411 Design and Analysis of Algorithms Set 2: Sorting Lower Bound Prof. Jennifer Welch Fall 2008.
CPSC 411, Fall 2008: Set 2 1 CPSC 311 Analysis of Algorithms Sorting Lower Bound Prof. Jennifer Welch Fall 2009.
Divide and Conquer Sorting
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
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
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.
Lower Bounds for Comparison-Based Sorting Algorithms (Ch. 8)
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Computer Algorithms Lecture 11 Sorting in Linear Time Ch. 8
Sorting in Linear Time Lower bound for comparison-based sorting
CSE 373 Data Structures Lecture 15
1 More Sorting radix sort bucket sort in-place sorting how fast can we sort?
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.
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.
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
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
Bucket & Radix Sorts. Efficient Sorts QuickSort : O(nlogn) – O(n 2 ) MergeSort : O(nlogn) Coincidence?
Mudasser Naseer 1 11/5/2015 CSC 201: Design and Analysis of Algorithms Lecture # 8 Some Examples of Recursion Linear-Time Sorting Algorithms.
CSC 213 Lecture 13: Writing Code & Sorting Lowest Bound.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
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,
Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.
1 Algorithms CSCI 235, Fall 2015 Lecture 17 Linear Sorting.
Data Structures Haim Kaplan & Uri Zwick December 2013 Sorting 1.
CSCE 411H Design and Analysis of Algorithms Set 10: Lower Bounds Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 10 1 * Slides adapted.
Analysis of Algorithms CS 477/677 Lecture 8 Instructor: Monica Nicolescu.
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.
What is the runtime of the best possible (comparison based) sorting algorithm? 1.O(log n) 2.O(n) 3.O(n log n) 4.O(n 2 ) 5.None of the above.
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.
CPSC 411 Design and Analysis of Algorithms
Introduction to Algorithms
CSCE 411 Design and Analysis of Algorithms
CSCE 411 Design and Analysis of Algorithms
CS200: Algorithm Analysis
Linear Sorting Sorting in O(n) Jeff Chastine.
Lower Bounds & Sorting in Linear Time
Linear Sorting Section 10.4
Linear-Time Sorting Algorithms
Lower bound for sorting, radix sort
CPSC 411 Design and Analysis of Algorithms
Sorting We have actually seen already two efficient ways to sort:
Presentation transcript:

1 Today’s Material Lower Bounds on Comparison-based Sorting Linear-Time Sorting Algorithms –Counting Sort –Radix Sort

2 How Fast Can We Sort? Basic Sorting Algorithms –BubleSort, InsertionSort and SelectionSort all run in O(N^2) Fast Sorting Algorithms –Mergesort, Heapsort and Quicksort all run in O(N log N) best case running time Can we do any better? –Can we come up with a sorting algorithm that will sort in O(N log logN)? What about O(N)?

3 Lower Bound on Comparison-based Sorting Recall our basic assumption: we can only compare two elements at a time –Suppose you are given N elements –How many possible orderings can you get? Example: a, b, c (N = 3) How many distinct sequences exist? Orderings: 1.a b c 2.b c a 3.c a b 4.a c b 5.b a c 6.c b a N = 3: We have 6 orderings = 321 = 3! N choices N-1 choices N-2 choices 1 choice For N elements: –  N! orderings

4 A “Decision Tree” for Sorting N=3 Elements Possible Orderings a < b < c, b < c < a, c < a < b, a < c < b, b < a < c, c < b < a Remaining Orderings a < b < c c < a < b a < c < b b < c < a b < a < c c < b < a a < b a > b b < c < a b < a < c c < b < a b < c < a b < a < c a < c a > c a < b < c a < c < b c < a < b b > c b < c a < b < c a < c < b b < c b > c a > c a < c Leaves contain all possible orderings of a, b, c

5 Decision Trees and Sorting A Decision Tree for Sorting is a Binary Tree such that: –Each node = a set of orderings –Each edge = 1 comparison –Each leaf = 1 unique ordering How many leaves for N distinct elements? –Only 1 leaf has correct sorted ordering for given a, b, c Each sorting algorithm corresponds to a decision tree –Finds correct leaf by following edges (= comparisons) Run time >= maximum no. of comparisons –Depends on: depth of decision tree –What is the depth of a decision tree for N distinct elements?

6 Decision Trees and Sorting Suppose you have a binary tree of depth d. How many leaves can the tree have? –E.g. Depth = 1  at most 2 leaves –Depth = 2  at most 4 leaves, etc. –Depth = d  at most 2 d leaves. Easy to prove. Number of leaves L = log(L) –Decision tree has L = N! leaves –Depth d >= log(N!) –What is log(N!)? log(N!) = log N + log(N-1) + … log(N/2) + … + log 1 >= log N + log(N-1) + … log(N/2) (N/2 terms only) >= (N/2)log(N/2) = (N log N) Result: Any sorting algorithm based on comparisons between elements requires (N log N) comparisons

7 Using the Lower Bound for Sorting Lower bound that we proved for sorting is in fact used to prove lower bound for other problems. Here is how: –Say you have a problem for which you want to prove a lower bound –If we can reduce the sorting problem to the problem to be solved, then we have essentially proved that the lower bound for the new problem is nlogn. Why? Because if we can solve your problem in less time than nlogn, then we can solve sorting in less time than nlogn. And we have just proved that that is not possible!

8 Example  (NlogN) Problems Convex Hull –Given a set of points in the plane, find the closest convex polygon that encloses them Closest Pair –Given a list of n elements, which pair are closest in value Element Uniqueness –Given a list of n numbers, are there any duplicates in the list

9 What about Counting Sort? Problem: Sort integers in the range 1 to B Algorithm: 1.Allocate an array Count having B slots (“buckets”) 2.Initialize: Count[i] = 0 for i = 1 to B 3.Given input integer i, Count[i]++ 4.After reading all inputs, scan Count[i] for i = 1 to B and print i if Count[i] is non-zero Ex: Sort the following integers in the range 1 to 9 –

10 What if A holds records with integer key? // A[1..n]: Holds the initial input. A[I].key is the integer // key value on which to sort on. A[I].data is the other satellite data // B[1..n]: Holds the sorted output // Count[1..B]: Count[I] is the rank of I, that is, the number of // elements of A whose key value is less than or equal to i CountingSort(A, B, n, B) { for I=1 to B do Count[I] = 0; for j=1 to n do Count[A[j].key]++; for I=2 to B do Count[I] += Count[I-1]; for j=n downto 1 do { I = A[j].key; B[Count[I]] = A[j]; Count[I]--; } //end-for } //end-CountingSort

11 Counting Sort Example aers v A v B sv B srv B srv e B asrv e B 0224

12 Counting Sort Running Time What is the running time for sorting N integers using bucket sort? –Running Time: O(B+N) –B to zero/scan the array and N to read the input –If B is O(N), then running time for Bucket sort = O(N) –Doesn’t this violate the (N log N) lower bound result? –No – When we do Count[i]++, we are comparing one element with all B elements, not just two elements Not regular 2-way comparison-based sorting

13 Radix Sort: Stable Counting Sort Problem: What if number of buckets needed is too large? Recall: Stable sort = a sort that does not change order of items with same key Radix sort = stable bucket sort on “slices” of key 1.Divide integers/strings into digits/characters 2.Bucket-sort from least significant to most significant digit/character –Uses linked lists –Stability ensures keys already sorted stay sorted –Takes O(P(B+N)) time where P = number of digits

14 Radix Sort Example [4] 19[4] 95[4] 57[6] 29[6] 17[6] 27[8] 9[5]4 5[7]6 1[7]6 2[7]8 4[9]4 1[9]4 2[9]6 [1]76 [1]94 [2]78 [2]96 [4]94 [5]76 [9]

15 Radix Sort Running Time P = # of digits iterations over the data set Each iteration takes O(B+N) time –Total: O(P(B+N)) time where P = number of digits E.g., N numbers in base 10 in the range (max. 6 digits) –Total: O(6*(10+N)) –Total: O(60 + 6N)

16 Summary of Sorting Sorting choices: –O(N 2 ) – Bubblesort, Selection Sort, Insertion Sort –O(N x ) – Shellsort (x = 3/2, 4/3, 2, etc. depending on incr. seq.) –O(N log N) average case running time: Mergesort: easy to code but uses O(N) extra space Heapsort: needs 2 comparisons to move data (between 2 children and parent) – may not be fast in practice Quicksort: fastest in practice but trickier to code, O(N 2 ) worst case –O(P·N) Radix sort (using Counting sort) for special cases where keys are P digit integers/strings