Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 5 Linear-time sorting Can we do better than comparison sorting? Linear-time 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
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.
CSCE 3110 Data Structures & Algorithm Analysis
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
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.
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.
Lecture 5: Master Theorem and Linear Time Sorting
Analysis of Algorithms CS 477/677
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.
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.
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.
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.
Foundations of Data Structures Practical Session #12 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.
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
Linear-Time Sorting Continued Medians and Order Statistics
MCA 301: Design and Analysis of Algorithms
Introduction to 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
Lecture 5 Algorithm Analysis
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
Lecture 5 Algorithm Analysis
Algorithms CSCI 235, Spring 2019 Lecture 18 Linear Sorting
The Selection Problem.
Lecture 5 Algorithm Analysis
Presentation transcript:

Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 5 Linear-time sorting Can we do better than comparison sorting? Linear-time sorting algorithms: –Counting-Sort –Radix-Sort –Bucket-sort

Data Structures, Spring 2004 © L. Joskowicz 2 Linear time sorting With more information (or assumptions) about the input, we can do better than comparison sorting. Consider sorting integers. Additional information/assumption: –Integer numbers in the range [0..k] where k = O(n). –Real numbers in the range [0,1) distributed uniformly Three algorithms: –Counting-Sort –Radix-Sort –Bucket-Sort

Data Structures, Spring 2004 © L. Joskowicz 3 Counting sort Input: n integer numbers in the range [0..k] where k is an integer and k = O(n). The idea: determine for each input element x its rank: the number of elements less than x. Once we know the rank r of x, we can place it in position r+1 Example: if there are 6 elements smaller than 17, then we can place 17 in the 7 th position. Repetitions: when there are several elements with the same value, locate them one after the other in the order in which they appear in the input  this is called stable sorting,

Data Structures, Spring 2004 © L. Joskowicz 4 Counting sort: intuition (1) When there are no repetitions and n = k, Rank[A[i]] = A[i] and B[Rank[A[i]]  A[i] For each A [ i ], count the number of elements ≤ to it. This rank of A [ i ] is the index indicating where it goes 4125 A = B = Rank =

Data Structures, Spring 2004 © L. Joskowicz 5 Counting sort: intuition (2) 5123 A = 3234 Rank = When there are no repetitions and n < k, some cells are unused, but the indexing still works. B =

Data Structures, Spring 2004 © L. Joskowicz 6 Counting sort: intuition (3) 4122 A = 1435 Rank = When n > k or there are repetitions, place them one after the other in the order in which they appear in the input and adjust the index by one  this is called stable sorting B =

Data Structures, Spring 2004 © L. Joskowicz 7 Counting sort Counting-Sort(A, B, k) 1.for i  0 to k 2. do C[i]  0 3.for j  1 to length[A] 4. do C[A[j]]  C[A[j]] +1 5./* now C contains the number of elements equal to i 6.for i  1 to k 7. do C[i]  C[i] + C[i –1] 8./* now C contains the number of elements ≤ to i 9.for j  length[A] downto do B[C[A[j]]]  A[j] /* place element 11. C[A[j]]  C[A[j]] – 1 /* reduce by one A[1..n] is the input array B [1..n] is the output array C [0..k] is a counting array

Data Structures, Spring 2004 © L. Joskowicz 8 Counting sort example (1) n = 8 k = 6 B[C[A[j]]]  A[j] C[A[j]]  C[A[j]] – 1 after line 11 C[A[j]]  C[A[j]] +1 after line 4 C = A = C = C[i]  C[i] + C[i –1] after line C = B = 3 6

Data Structures, Spring 2004 © L. Joskowicz 9 Counting sort example (2) 0 3 B = C = A = C =

Data Structures, Spring 2004 © L. Joskowicz 10 Counting sort example (3) 0 3 B = C = A = C =

Data Structures, Spring 2004 © L. Joskowicz 11 Counting sort: complexity analysis for loop in lines 1—2 takes Θ(k) for loop in lines 3—4 takes Θ(n) for loop in lines 6—7 takes Θ(k) for loop in lines 9 –11 takes Θ(n) Total time is thus Θ(n+k) Since k = O(n), T(n) = Θ(n) and S(n) = Θ(n)  the algorithm is optimal!! This does not work if we do not assume k = O(n). Wasteful if k >> n and is not sorting in place.

Data Structures, Spring 2004 © L. Joskowicz 12 Radix sort Input: n integer numbers with d digits The idea: look at one digit at a time and sort the numbers according to this digit only. Start from the least significant digit, working up to the most significant one. Since there are only 10 different digits 0..9, there are only 10 places used for each column. For example, we can use Counting-Sort for each call, with k = 9. In general, k << n, so k = O(n). At the end, the numbers will be sorted!!

Data Structures, Spring 2004 © L. Joskowicz 13 Radix sort: example

Data Structures, Spring 2004 © L. Joskowicz 14 Radix-Sort Radix-Sort(A, d) 1.for i  1 to d 2. do use a stable sort to sort array A on digit d Notes: Complexity: T(n) = Θ(d(n+k))  Θ(n) for constant d and k = O(1) Every digit is in the range [0..k –1] and k = O(1) The sorting MUST be a stable sort, otherwise it fails! This algorithm was invented to sort computer punched cards!

Data Structures, Spring 2004 © L. Joskowicz 15 Proof of correctness of Radix-Sort (1) We want to prove that Radix-Sort is a correct stable sorting algorithm Proof: by induction on the number of digits d. Let x be a d-digit number. Define x l as the number formed by the last l digits of x, for l ≤ d. For example, x = 2345 then x 1 = 5, x 2 = 45, x 3 = 345… Base: for d = 1, Radix-Sort uses a stable sorting algorithm to sort n numbers in the range [0..9]. So if x 1 < y 1, x will appear before y. When x 1 = y 1, the positions of x and y will not be changed since stable sorting was used.

Data Structures, Spring 2004 © L. Joskowicz 16 Proof of correctness of Radix-Sort (2) General case: assume Radix sorting is correct after i –1 < d passes, the numbers x i–1 are sorted in stable sort order Assume x i < y i. There are two cases: 1. The i th digit of x < i th digit of y Radix-Sort will put x before y, so it is OK. 2. The i th digit of x = i th digit of y By the induction hypothesis, x i–1 < y i–1, so x appears before y before the iteration and since the i th digits are the same, their order will not change in the new iteration, so they will remain in the same order.

Data Structures, Spring 2004 © L. Joskowicz 17 Proof of correctness of Radix-Sort (3) Assume now x i = y i. All the digits that have been sorted are the same. By induction, x and y remain in the same order they appeared before the ith iteration, and snde the ith iteration is stable, they will remain so after the additional iteration. This completes the proof!

Data Structures, Spring 2004 © L. Joskowicz 18 Properties of Radix-Sort Given n b-bit numbers and a number r ≤ b. Radix- Sort will take Θ((b/r)(n+2 r )) Take d = b/r digits of r bits each in the range [0..2 r –1], so we can use Counting-Sort with k = 2 r –1. Each pass of Counting-Sort takes Θ(n+k) so we get Θ(n+2 r ) and there are d passes, so the total running time is Θ(d(n+2 r )), or Θ((b/r)(n+2 r )). For given values of n and b, we can choose r ≤ b to be optimum  minimize Θ((b/r)(n+2 r )). Choose r = lg n to get Θ(n).

Data Structures, Spring 2004 © L. Joskowicz 19 Bucket sort Input: n real numbers in the interval [0..1) uniformly distributed (numbers have equal probability) The idea: Divide the interval [0..1) into n buckets 0, 1/n, 2/n. … (n–1)/n. Put each element a i into its matching bucket 1/i ≤ a i ≤ 1/(i+1). Since the numbers are uniformly distributed, not too many elements will be placed in each bucket. If we insert them in order (using Insertion-Sort), the buckets and the elements in them will always be in sorted order.

Data Structures, Spring 2004 © L. Joskowicz 20 Bucket sort: example

Data Structures, Spring 2004 © L. Joskowicz 21 Bucket-Sort Bucket-Sort(A) 1. n  length(A) 2. for i  0 to n 3. do insert A[i] into list B[floor(nA[i])] 4.for i  0 to n –1 5. do Insertion-Sort(B[i]) 6.Concatenate lists B[0], B[1], … B[n –1] in order A[i] is the input array B[0], B[1], … B[n –1] are the bucket lists

Data Structures, Spring 2004 © L. Joskowicz 22 Bucket-Sort: complexity analysis All lines except line 5 (Insertion-Sort) take O(n) in the worst case. In the worst case, O(n) numbers will end up in the same bucket, so in the worst case, it will take O(n 2 ) time. However, in the average case, only a constant number of elements will fall in each bucket, so it will take O(n) (see proof in book). Extensions: use a different indexing scheme to distribute the numbers (hashing – later in the course!)

Data Structures, Spring 2004 © L. Joskowicz 23 Summary With additional assumptions, we can sort n elements in optimal time and space Ω(n).