Introduction to Algorithms

Slides:



Advertisements
Similar presentations
Sorting in Linear Time Introduction to Algorithms Sorting in Linear Time CSE 680 Prof. Roger Crawfis.
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
Linear Sorts Counting sort Bucket sort Radix sort.
MS 101: Algorithms Instructor Neelima Gupta
1 Sorting in Linear Time How can we do better?  CountingSort  RadixSort  BucketSort.
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.
CSC 211 Data Structures Lecture 20
Lower bound for sorting, radix sort COMP171 Fall 2005.
CSCE 3110 Data Structures & Algorithm Analysis
Lower bound for sorting, radix sort COMP171 Fall 2006.
1 SORTING Dan Barrish-Flood. 2 heapsort made file “3-Sorting-Intro-Heapsort.ppt”
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.
Lecture 5: Master Theorem and Linear Time Sorting
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.
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
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.
Introduction to Algorithms Jiafen Liu Sept
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.
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.
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 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.
Lower Bounds & Sorting in Linear Time
Sorting.
Linear-Time Sorting Continued Medians and Order Statistics
MCA 301: Design and Analysis of Algorithms
Algorithm Design and Analysis (ADA)
CS200: Algorithm Analysis
Lecture 5 Algorithm Analysis
Linear Sorting Sections 10.4
Keys into Buckets: Lower bounds, Linear-time sort, & Hashing
Chapter 8-2: Sorting in Linear Time
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.
CS200: Algorithm Analysis
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
The Selection Problem.
Linear Time Sorting.
Lecture 5 Algorithm Analysis
Presentation transcript:

Introduction to Algorithms Counting Sort

Sorting So Far Insertion Sort Easy to code Fast on small inputs (less than ~ 50 elements) Fast on nearly-sorted inputs O(n2) worst case O(n2) average (equally-likely inputs) case O(n2) reverse-sorted case

Sorting So Far Merge Sort Divide-and-Conquer O(n lg n) worst case Split array in half Recursively sort subarrays Linear-time merge step O(n lg n) worst case It does not sort in place

Sorting So Far Heap Sort It uses the very useful heap data structure Complete binary tree Heap property: parent key > children’s keys O(n lg n) worst case It sorts in place

How Fast Can We Sort? We will provide a lower bound, then beat it How do you suppose we’ll beat it? First, an observation: all of the sorting algorithms so far are comparison sorts The only operation used to gain ordering information about a sequence is the pairwise comparison of two elements Theorem: all comparison sorts are Ω(n lg n)

Decision Trees 5 7 3 5 73 37 35 35 7

Decision Trees Decision trees provide an abstraction of comparison sorts A decision tree represents the comparisons made by a comparison sort. What do the leaves represent? How many leaves must there be?

Decision Trees Decision trees can model comparison sorts For a given algorithm: One tree for each n Tree paths are all possible execution traces What’s the longest path in a decision tree for insertion sort? For merge sort? What is the asymptotic height of any decision tree for sorting n elements? Answer: Ω(n lg n) (Now let’s prove it…)

A Lower Bound for Comparison Sorts Thus the time to comparison sort n elements is Ω(n lg n) Corollary: Heapsort and Mergesort are asymptotically optimal comparison sorts But the name of this lecture is “Sorting in linear time”! How can we do better than Ω(n lg n)?

Sorting in Linear Time Counting Sort Basic Idea Algorithm Storage No comparisons between elements ! But…depends on assumption about the numbers being sorted Basic Idea For each input element x, determine the number of elements less than or equal to x For each integer i (0 ≤ i ≤ k), count how many elements whose values are i Then we know how many elements are less than or equal to i Algorithm Storage A[1..n]: input elements B[1..n]: sorted elements C[0..k]: hold the number of elements less than or equal to i

Counting Sort Illustration (Range from 0 to 5) Spring 2006 Algorithm Networking Laboratory 5-15/55

Counting Sort Illustration 2 5 3 0 2 3 0 3 B 3 3 C 1 4 5 7 8 3 5 7 0 2 3 8 C 0 2 3 5 7 8 C 0 2 3 4 7 8 C 0 2 32 4 7 7 B 2 3 3 3 8 C 3 4 7 7

Counting Sort Θ(k) Θ(n) Θ(n+k)

Counting Sort Total time: O(n + k) But sorting is Ω(n lg n)! Usually, k = O(n) Thus counting sort runs in O(n) time But sorting is Ω(n lg n)! No contradiction This is not a comparison sort In fact, there are no comparisons at all ! Notice that this algorithm is stable

Bucket Sort Bucket sort Assumption: the keys are in the range [0, N) Basic idea: 1. Create N linked lists (buckets) to divide interval [0,N) into subintervals of size 1 2. Add each input element to appropriate bucket 3. Concatenate the buckets Expected total time is O(n + N), with n = size of original sequence if N is O(n)  sorting algorithm in O(n) !

Bucket Sort Each element of the array is put in one of the N “buckets”

Bucket Sort Now, pull the elements from the buckets into the array At last, the sorted array (sorted in a stable way):

Does it Work for Real Numbers? What if keys are not integers? Assumption: input is n reals from [0, 1) Basic idea: Create N linked lists (buckets) to divide interval [0,1) into subintervals of size 1/N Add each input element to appropriate bucket and sort buckets with insertion sort Uniform input distribution  O(1) bucket size Therefore the expected total time is O(n) Distribution of keys in buckets similar with …. ?

Radix Sort How did IBM get rich originally? Answer: punched card readers for census tabulation in early 1900’s. In particular, a card sorter that could sort cards into different bins Each column can be punched in 12 places (Decimal digits use only 10 places!) Problem: only one column can be sorted on at a time

Radix Sort Intuitively, you might sort on the most significant digit, then the second most significant, etc. Problem: lots of intermediate piles of cards to keep track of Key idea: sort the least significant digit first RadixSort(A, d) for i=1 to d StableSort(A) on digit i

Radix Sort Can we prove it will work? Inductive argument: Assume lower-order digits {j: j<i}are sorted Show that sorting next digit i leaves array correctly sorted If two digits at position i are different, ordering numbers by that digit is correct (lower- order digits irrelevant) If they are the same, numbers are already sorted on the lower-order digits. Since we use a stable sort, the numbers stay in the right order

Radix Sort What sort will we use to sort on digits? Bucket sort is a good choice: Sort n numbers on digits that range from 1..N Time: O(n + N) Each pass over n numbers with d digits takes time O(n+k), so total time O(dn+dk) When d is constant and k=O(n), takes O(n) time

Radix Sort Example Problem: sort 1 million 64-bit numbers Treat as four-digit radix 216 numbers Can sort in just four passes with radix sort! Running time: 4( 1 million + 216 )  4 million operations Compare with typical O(n lg n) comparison sort Requires approx lg n = 20 operations per number being sorted Total running time  20 million operations

Radix Sort Sort N numbers, each with k bits Cpt S 223 Radix sort achieves stable sorting To sort each column, use counting sort (O(n)) => To sort k columns, O(nk) time Radix Sort Sort N numbers, each with k bits E.g, input {4, 1, 0, 10, 5, 6, 1, 8} 4 0100 1 0001 0 0000 10 1010 5 0101 6 0110 8 1000 0100 0000 1010 0110 1000 0001 0101 0100 0000 1000 0001 0101 1010 0110 0000 1000 0001 1010 0100 0101 0110 0000 4 0001 1 0100 4 0101 5 0110 6 1000 8 1010 10 lsb msb Washington State University

Radix Sort In general, radix sort based on bucket sort is Asymptotically fast (i.e., O(n)) Simple to code A good choice Can radix sort be used on floating-point numbers?

Summary: Radix Sort Radix sort: Assumption: input has d digits ranging from 0 to k Basic idea: Sort elements by digit starting with least significant Use a stable sort (like bucket sort) for each stage Each pass over n numbers with 1 digit takes time O(n+k), so total time O(dn+dk) When d is constant and k=O(n), takes O(n) time Fast, Stable, Simple Doesn’t sort in place