Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
How fast can we sort?
Decision-tree example
Lower Bound for decision-tree sorting
Lower bound for comparison sorting
Sorting in linear time Counting something, like histograms, can help us to sort an array. –clr = getcolor(x,y) –count[ clr ]++; Limitation: count[0..255]
Sorting in linear time
Counting sort COUNTING-SORT(A,B,k) // Initialize aux. store // Count how many times occurred. // Cumulative sums of aux. store // ???
Counting-sort example
Analysis
Running Time
Stable sorting
Counting sort array size limitation problem If we need sort 32 bits integers, size of count array 2 32 = 4GB ! Unsorted array size is usually smaller than this (1KB << 4GB) Solution... Use smaller parts like digits and sort these parts particularly : Radix sort
Radix sort
Operation of radix sort
Analysis of radix sort Given n d-digit number in which each digit can take on up to k-possible values, the running time of RADIX-SORT sorts these numbers is (d*(n+k))
Analysis of radix sort Assume counting sort is the auxilary sort. Sort n computer words of b-bits each. Each word can be viewed as having d=b/r (base-2 r ) digits. Example: Each key having d-digits of r bits. Let d=4, r=8 (b=32) then each digit is an integer in the range 0 to 2 r -1 = =255. r=8
Analysis of radix sort: Choosing r How many passes should we make? Each word of length b=rd bit is broken into r-bit pieces. For a given word length of b, the running time of an array of length n that is T(r|n,b) can be minimized with respect to r.
Analysis of radix sort: Choosing r
Conclusions
What about floating points? Integer can countable or divisible to parts. How can speed up sorting array of floating point numbers. We can group into k-parts –For numbers beetween 0.0 ≤ f ≤ 1.0 –k*f have an integer part like 8.71 –int(k*f) is part number. –k can be length of array.
Bucket Sort
A B 9.17 / /.26 /.68 / /
Bucket Sort Running time:
Bucket Sort
Order Statistics
Hairy recurrence (cont.)