2. خان سنور Algorithm Analysis
Counting Sort
Linear Time Sorting
Bucket Sort
Radix Sort
Contents
3. خان سنور Algorithm Analysis
Linear Time Sorting
• There are sorting algorithms that run faster than O(n lg n) time but
they require special assumptions about the input sequence to be sort.
• Examples of sorting algorithms that run in linear time are counting
sort, radix sort and bucket sort.
• Counting sort and radix sort assume that the input consists of integers
in a small range. Whereas, bucket sort assumes that the input is
generated by a random process that distributes elements uniformly
over the interval.
• Since we already know that the best comparison-based sorting can to
is Ω(n lg n).
• It is not difficult to figure out that linear-time sorting algorithms use
operations other than comparisons to determine the sorted order.
4. خان سنور Algorithm Analysis
Linear Time Sorting
• Despite of linear time usually these algorithms are not very desirable
from practical point of view. Firstly, the efficiency of linear-time
algorithms depend on the keys randomly ordered.
• If this condition is not satisfied, the result is the degrading in
performance. Secondly, these algorithms require extra space
proportional to the size of the array being sorted, so if we are dealing
with large file, extra array becomes a real liability. Thirdly, the
"inner-loop" of these algorithms contain quite a few instructions, so
even though they are linear, they would not be as faster than quick
sort (say).
6. خان سنور Algorithm Analysis
Counting Sort
• Counting sort assumes that each of the elements is an integer in the
range 1 to k, for some integer k. When k = O(n), the Counting-sort
runs in O(n) time.
• The basic idea of Counting sort is to determine, for each input
elements x, the number of elements less than x. This information can
be used to place directly into its correct position.
• For example, if there 17 elements less than x, than x belongs in
output position 18.
• In the code for Counting sort, we are given array A[1 . . n] of
length n. We required two more arrays, the array B[1 . . n] holds the
sorted output and the array c[1 . . k] provides temporary working
storage.
7. خان سنور Algorithm Analysis
Implementation
1. for i ← 0 to k do
2. c[i] ← 0
3. for j ← 0 to n do
4. c[A[j]] ← c[A[j]] + 1
5. //c[i] now contains the number of elements equal to i
6. for i ← 1 to k do
7. c[i] ← c[i] + c[i-1]
8. // c[i] now contains the number of elements ≤ i
9. for j ← n-1 downto 0 do
10. B[c[A[i]]] ← A[j]
11. c[A[i]] ← c[A[j]] - 1
8. خان سنور Algorithm Analysis
Running Example
1. for i ← 1 to k do
2. c[i] ← 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
5 9 4 5 3 6 8 5 4 8 7 4 6 9 7
0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0 0
A
C