Sorting in Linear Time: - Comparison Sort: - Non Comparison Sort
Sorting in Linear Time: - Comparison Sort: - Non Comparison Sort
• Comparison sort:
– Lower bound: (nlgn).
• Non comparison sort:
– Bucket sort, counting sort, radix sort
– They are possible in linear time (under certain
assumption).
Bucket Sort
• Assumption: uniform distribution
– Input numbers are uniformly distributed in [0,1).
– Suppose input size is n.
• Idea:
– Divide [0,1) into n equal-sized subintervals (buckets).
– Distribute n numbers into buckets
– Expect that each bucket contains few numbers.
– Sort numbers in each bucket (insertion sort as default).
– Then go through buckets in order, listing elements,
BUCKET-SORT(A)
1. n length[A]
2. for i 1 to n
3. do insert A[i] into bucket B[nA[i]]
4. for i 0 to n-1
5. do sort bucket B[i] using insertion sort
6. Concatenate bucket B[0],B[1],…,B[n-1]
Example of BUCKET-SORT
Analysis of BUCKET-SORT(A)
1. n length[A] (1)
2. for i 1 to n O(n)
3. do insert A[i] into bucket B[nA[i]] (1) (i.e. total O(n))
4. for i 0 to n-1 O(n)
5. do sort bucket B[i] with insertion sort O(ni2) ( i=0
n-1 O(ni2))
Total cost is (k+n), suppose k=O(n), then total cost is (n). Beat (nlg n).
Radix sort
• Suppose a group of people, with last name, middle, and
first name (each has one letter).
• For example: (z, x, k), (z,j,y), (f,s,f), …
• Sort it by the last name, then by middle, finally by the first
name
• Solution 1:
– sort by last name first as into (possible) 26 bins,
– Sort each bin by middle name into (possible) 26 more bins (26*26
=512)
– Sort each of 512 bins by the first name into 26 bins
• So if many names, there may need possible 26*26*26 bins.
• Suppose there are n names, there need possible n bins.