Non-Comparison Based Sorting

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:
Linear Sorts Counting sort Bucket sort Radix sort.
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.
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.
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
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.
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.
Sorting Heapsort Quick review of basic sorting methods Lower bounds for comparison-based methods Non-comparison based sorting.
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.
Lecture 5: Master Theorem and Linear Time Sorting
CSE 326: Data Structures Sorting Ben Lerner Summer 2007.
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
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 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
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
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.
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.
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.
Sorting & Lower Bounds Jeff Edmonds York University COSC 3101 Lecture 5.
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.
Advanced Sorting 7 2  9 4   2   4   7
Lower Bounds & Sorting in Linear Time
Sorting.
Linear-Time Sorting Continued Medians and Order Statistics
Introduction to Algorithms
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
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
Linear-Time Sorting Algorithms
Lecture 5 Algorithm Analysis
Algorithms CSCI 235, Spring 2019 Lecture 18 Linear Sorting
Linear Time Sorting.
Lecture 5 Algorithm Analysis
Presentation transcript:

Non-Comparison Based Sorting

How fast can we sort? Insertion Sort: O(n2) Merge Sort, Quick Sort (expected), Heap Sort: O(nlgn) Can we sort faster than O(nlgn)?

Comparison-Based Sort Can prove that any comparison-based sort MUST make Ω(nlgn) comparisons. Comparison-based sort: algorithm directly compares elements of the array to one another, makes some decision based upon <, =, > This means that heapsort and mergesort are optimal. Not always possible to prove that your algorithm is the best possible! Proof idea Use a decision tree to represent comparisons of a sorting algorithm Actual sorting algorithm maps to the decision tree

Decision Tree Example Three elements to sort: a1, a2, a3 Decision tree must have n! leaves, one for each permutation of the input

Decision Tree For any comparison-based sorting algorithm One tree exists for each input of length n An algorithm “splits” at each decision/comparison unwinding the actual execution into a tree path The tree is for all possible execution traces for an input of length n Height of the tree tells us the minimum number of comparisons necessary to sort the input There must be n! leaves But we also have a binary tree; binary tree of height h has no more than 2h leaves This means that n!  2h

Height of Tree n!  2h lg(n!)  lg(2h) Stirlings approximation says: Giving us: This means h is Ω(nlgn)

Better than O(nlgn)? This means any comparison-based sorting algorithm must require at least time O(nlgn) But we can do better if we use a non-comparison-based sorting algorithm! Count Sort Radix Sort Bucket Sort Postman Sort

Count Sort May run in O(n) time But requires assumptions about the size and nature of the input Input: A[1..n] where a[i] is an integer in the range 1..k Output: B[1..n], array input values sorted Uses: C[1..k] auxiliary storage Idea: Using the random access of the array C, count up the number of times each input element appears and then collect them together

Count Sort Algorithm Count-Sort(A,n) for i=1 to k do C[i]=0 ; Initialize to 0 for j=1 to n do C[A[j]] ++ ; Count j=1 for i=1 to k do if (C[i]>0) then for z=1 to C[i] do B[j]=i j++ Ex: A=[1 5 3 2 2 4 9] ; Initial values C=[0 0 0 0 0 0 0 0 0] ; Initialize C to 0 C=[1 2 1 1 1 0 0 0 1] ; After count loop B=[1 2 2 3 4 5 9] ; After last loop Runtime: O(n+k). If k is close to n, then runs in O(n) time. A bad example would be a list such as: A[1,2,999999999999]

Stable Count Sort A sorting algorithm is stable if the occurrence of a value I appears in the same value in the output as they do in the input Ties broken by the rule of whichever appeared first in the input appears first in the output Desired for various algorithms where sorting a key of a record (e.g. a zip code) Example: A[3,5a,9,2,4,5b,6] Sorts to : A[2,3,4,5a,5b,6,9] not A[2,3,4,5b,5a,6,9] Prior algorithm was not stable but we can modify to make it stable

Stable Count Sort Key idea: fetch from original array to make it stable.

Radix Sort Works like the punch card readers of the early 1900’s Only works on input items with digits E.g. numbers, letters Idea: Sort on the least significant digit first ; Array A of n elements, each element d digits Radix-Sort(A,d,n) for i = 1 to d Stable-Sort(A) with digit i as the key

Radix Sort Example

Radix Sort Notes Internal sort must be stable to preserve order of elements. Based on the observation that if all the lower order digits are already sorted, then we only need to sort on the highest order digit. Good choice to use if each digit is small in magnitude If k is the maximum value of a digit If using stable count sort internally Stable-Count-Sort takes Θ(k+n) time for one pass With d passes total, runtime is Θ(dk+dn) Runtime is Θ(n) if d is a constant, k is same magnitude as n

Bucket Sort Bucket sort works similarly to count sort, but uses a “bucket” to hold a range of inputs. This allows it to operate on real numbers. Runs in O(n) time with the following assumptions: Input elements are in the interval [0..1]. If not, in many cases we can divide by some “max” value to scale the input to be between 0 and 1 Input elements are evenly distributed Uniform probability over [0..1]

Bucket Sort Algorithm Idea: Algorithm: Divide [0..1] into n equal sized “buckets” Put each of the n inputs into the proper bucket; some may be empty and others may have more than 1 element Sort each bucket using insertion sort To produce output, go through the buckets in order, listing the elements in each Algorithm: Bucket-Sort(A,n) for i=1 to n do ; implicitly make buckets 0..n-1 Insert A[i] into bucket B[n*A[i]] for i =0 to n-1 do sort bucket B[i] using insertion sort ; < O(n) if small bucket concatenate buckets B[0], B[1], .. B[n-1] in order

Bucket Sort Example

Bucket Sort Notes Expected runtime: If any element in A comes from [0..1] with equal probability, then the probability that an element e is in bucket B[i] is 1/n Expect on average each bucket to have a single element Call to insertion sort for each bucket will be extremely fast; essentially constant time Rest of the algorithm runs in O(n) Overall runtime O(n) if the uniform probabilty assumption holds Worst case O(n2) if everything ends up in one bucket

Postman Sort Algorithm proposed by Robert Ramey, August 1992 issue of C Programming Journal Analysis left as a homework exercise From the article: When a postal clerk receives a huge bag of letters he distributes them into other bags by state. Each bag gets sent to the indicated state. Upon arrival, another clerk distributes the letters in his bag into other bags by city. So the process continues until the bags are the size one man can carry and deliver. This is the basis for my sorting method which I call the postman's sort.

Postman Sort Idea Given a large list of records to sort alphabetically Make one pass, reading each record into one of 26 lists depending on the first letter in the field First list contains all records starting with “A” Second list contains all records starting with “B”, etc. Recurse on each of the 26 sublists if it contains more than one element, but this time using the next letter as the field to split up the sublists In-order traversal of the resulting tree gives a sorted list

Postman Example A = [bob, bill, boy, cow, dog] A = [bob, bill, boy] A = [cow] A = [dog] Empty for all other letters i o Empty for all other letters A = [bill] A = [bob, boy] b y Empty for all other letters A = [bob] A = [boy] Runtime? Left as a homework exercise