1 Sorting in O(N) time CS302 Data Structures Section 10.4.

Slides:



Advertisements
Similar presentations
Sorting in Linear Time Introduction to Algorithms Sorting in Linear Time CSE 680 Prof. Roger Crawfis.
Advertisements

Analysis of Algorithms
Sorting in linear time (for students to read) Comparison sort: –Lower bound:  (nlgn). Non comparison sort: –Bucket sort, counting sort, radix sort –They.
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.
CSE 3101: Introduction to the Design and Analysis of Algorithms
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
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.
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.
Tirgul 4 Subjects of this Tirgul: Counting Sort Radix Sort Bucket Sort.
Analysis of Algorithms CS 477/677 Midterm Exam Review Instructor: George Bebis.
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
Linear Sorts Chapter 12.3, Last Updated: :39 AM CSE 2011 Prof. J. Elder Linear Sorts?
David Luebke 1 10/13/2015 CS 332: Algorithms Linear-Time Sorting Algorithms.
CSC 41/513: Intro to Algorithms Linear-Time Sorting Algorithms.
Analysis of Algorithms CS 477/677
Targil 6 Notes This week: –Linear time Sort – continue: Radix Sort Some Cormen Questions –Sparse Matrix representation & usage. Bucket sort Counting sort.
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.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
Sorting Data Structures and Algorithms (60-254). Sorting Sorting is one of the most well-studied problems in Computer Science The ultimate reference on.
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.
Sorting algorithms General problem description: Given array A[0..n  1] with n elements that can be compared directly (i.e. for each i and j either A[i]
Foundations of Data Structures Practical Session #12 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.
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.
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
Introduction to Algorithms
Algorithm Design and Analysis (ADA)
Linear Sorting Sections 10.4
Keys into Buckets: Lower bounds, Linear-time sort, & Hashing
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
Algorithms CSCI 235, Spring 2019 Lecture 18 Linear Sorting
Presentation transcript:

1 Sorting in O(N) time CS302 Data Structures Section 10.4

2 How Fast Can We Sort? Selection Sort, Bubble Sort, Insertion Sort: Heap Sort, Merge sort: Quicksort: What is common to all these algorithms? –Make comparisons between input elements a i a j O(n 2 ) O(nlgn) O(nlgn) - average

3 Lower-Bound for Sorting Theorem: To sort n elements, comparison sorts must make  (nlgn) comparisons in the worst case. (see CS477 for a proof)

4 Can we do better? Linear sorting algorithms –Counting Sort –Radix Sort –Bucket sort Make certain assumptions about the data Linear sorts are NOT “comparison sorts”

5 Counting Sort Assumptions: –n integers which are in the range [0... r] –r is in the order of n, that is, r=O(n) Idea: –For each element x, find the number of elements x –Place x into its correct position in the output array output array

6 Step 1 (i.e., frequencies) (r=6)

7 Step 2 C new C (frequencies) (cumulative sums)

8 Algorithm Start from the last element of A Place A[i] at its correct place in the output array Decrease C[A[i]] by one A C new 8 0

9 Example A C new B B B B

10 Example (cont.) A B C B C B C B

11 COUNTING-SORT Alg.: COUNTING-SORT(A, B, n, k) 1.for i ← 0 to r 2. do C[ i ] ← 0 3.for j ← 1 to n 4. do C[A[ j ]] ← C[A[ j ]] C[i] contains the number of elements equal to i 6.for i ← 1 to r 7. do C[ i ] ← C[ i ] + C[i -1] 8. C[i] contains the number of elements ≤ i 9.for j ← n downto do B[C[A[ j ]]] ← A[ j ] 11. C[A[ j ]] ← C[A[ j ]] - 1 1n 0k A C 1n B j

12 Analysis of Counting Sort Alg.: COUNTING-SORT(A, B, n, k) 1.for i ← 0 to r 2. do C[ i ] ← 0 3.for j ← 1 to n 4. do C[A[ j ]] ← C[A[ j ]] C[i] contains the number of elements equal to i 6.for i ← 1 to r 7. do C[ i ] ← C[ i ] + C[i -1] 8. C[i] contains the number of elements ≤ i 9.for j ← n downto do B[C[A[ j ]]] ← A[ j ] 11. C[A[ j ]] ← C[A[ j ]] - 1 O(r) O(n) O(r) O(n) Overall time: O(n + r)

13 Analysis of Counting Sort Overall time: O(n + r) In practice we use COUNTING sort when r = O(n)  running time is O(n)

14 Radix Sort Represents keys as d-digit numbers in some base-k key = x 1 x 2...x d where 0≤x i ≤k-1 Example: key=15 key 10 = 15, d=2, k=10 where 0≤x i ≤9 key 2 = 1111, d=4, k=2 where 0≤x i ≤1

15 Radix Sort Assumptions d=O(1) and k =O(n) Sorting looks at one column at a time –For a d digit number, sort the least significant digit first –Continue sorting on the next least significant digit, until all digits have been sorted –Requires only d passes through the list

16 RADIX-SORT Alg.: RADIX-SORT (A, d) for i ← 1 to d do use a stable sort to sort array A on digit i (stable sort: preserves order of identical elements)

17 Analysis of Radix Sort Given n numbers of d digits each, where each digit may take up to k possible values, RADIX- SORT correctly sorts the numbers in O(d(n+k)) –One pass of sorting per digit takes O(n+k) assuming that we use counting sort –There are d passes (for each digit)

18 Analysis of Radix Sort Given n numbers of d digits each, where each digit may take up to k possible values, RADIX- SORT correctly sorts the numbers in O(d(n+k)) –Assuming d=O(1) and k=O(n), running time is O(n)

19 Bucket Sort Assumption: –the input is generated by a random process that distributes elements uniformly over [0, 1) Idea: –Divide [0, 1) into k equal-sized buckets (k=Θ(n)) –Distribute the n input values into the buckets –Sort each bucket (e.g., using quicksort) –Go through the buckets in order, listing elements in each one Input: A[1.. n], where 0 ≤ A[i] < 1 for all i Output: elements A[i] sorted

20 Example - Bucket Sort /.72 /.23 / /.68 /.39 / / / / / A B Distribute Into buckets

21 Example - Bucket Sort /.78 /.26 / /.68 /.39 / / / / / Sort within each bucket

22 Example - Bucket Sort /.78 /.26 / /.68 /.39 / / / / / / Concatenate the lists from 0 to n – 1 together, in order

23 Analysis of Bucket Sort Alg.: BUCKET-SORT(A, n) for i ← 1 to n do insert A[i] into list B[  nA[i]  ] for i ← 0 to k - 1 do sort list B[i] with quicksort sort concatenate lists B[0], B[1],..., B[n -1] together in order return the concatenated lists O(n) k O(n/k log(n/k)) =O(nlog(n/k) O(k) O(n) (if k=Θ(n))

24 Radix Sort as a Bucket Sort