Professional Documents
Culture Documents
Ch. 8 Linear Time (Single-Loop) Sorts
Ch. 8 Linear Time (Single-Loop) Sorts
index 1 2 3 4 5 6 7 8 9 10 11 12 13
Counter count 0 0, 0, 0 0, 0, 0 0, 0, 0, 0 0 0,
Array C 1 1, 1, 1, 1, 1 1, 1
with 2, 2 2, 2, 2,
history 3 3 3 3
Index 1 2 3 4 5 6 7 8 9 10 11 12 13
Counter
count 0 0, 0, 0 0, 0, 0 0, 0, 0, 0 0 0,
Array C 1, 1, 1, 1, 1, 1, 1, 1,
with 2, 2 2, 2, 2
history 3 3 3
A input 3 5 6 13 8 8 3 2 10 6 9 5 3 6 8 10
Index 1 2 3 4 5 6 7 8 9 10 11 12 13
mulative
ray count 0 1 3 0 2 3 0 3 1, 2 0 0 1,
Rank 0 1, 4, 4 6, 9, 9 12, 13, 15, 15 15 16,
0 3, 5, 8, 11, 12 14, 15
2, 4 7, 10, 13
1 6 9
index 1 2 3 4 5 6 7 8 9 10 11 12 13
Counter
Array C count 0 0 0 0 0 0 0 0 0 0 0 0 0
Counter Index 1 2 3 4 5 6 7 8 9 10 11 12 13
Array C count 0 1 3 0 2 3 0 3 1 2 0 0 1
Counter Index 1 2 3 4 5 6 7 8 9 10 11 12 13
Array to
cumulativ rank 0 1 4 4 6 9 9 12 13 15 15 15 16
e array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
B Output
2 3 3 3 5 5 6 6 6 8 8 8 9 10 10 16
Time complexity of Counting Sort
• Algo has 4 loops on three arrays of Size A[N], B[N]
and C[K] where N is number of elements and K is
range of elements.
• Obviously, if N>K i.e. Numbers are from a small
range are repeated also,
• T(N) = Theta(N)
• However, if K>N i.e. size of input is small but range
is larger. (e.g. 100 integers from range 1-1000000)
• Then T(N) = Theta(K) which may not be feasible.
• Roughly T(N) =Theta (Max(N, K))
• Or T(N)= Max(Theta(N), Theta(K))
• On the other hand, tie complexity of a good
divide-and-conquer is Theta(NLogN)
• Typically countingsort is feasible only when
K<NLogN, otherwise any Divide-and-conquer
sort is feasible.
Radix Sort
• Radix Sort was useful in sorting items using hardware
(old mechanical counters) and old digital IC’s.
• It sorts multidigit integers on the basis of their digit
positions.
• All elements are sorted on the basis of their
rightmost(least significant) digit in first step.
– Any stable sort technique can be used to sort single digit.
• One digit is sorted in one loop, then next digit and
then next digit is sorted, until leftmost(most
significate) digit is sorted.
• Eventually whole array is sorted.
• As number of digit is very small (2-6 etc) and digit
range is also small (0 to 9), such sorting becomes
handy.
• Radix sort uses any other sort to sort a single
digit, and depends upon time required to run that
sort. The other sort may or may not be a single
loop/nestedloop/divide&conquer sort.
– Radix Sort repeats itself for each digit Least-significant
to most significant, so its not in true sense linear sort.
Simulation of Radix Sort
Input First Pass 2nd Pass Last Pass
Algo
Time complexity of Radix-Sort
• Radix sort uses repeated sorts in a loop.
• Loop depends upon number of digits of input.
• Any stable sort can be used for internal sort of single
digits.
– As digit range is just 10, counting-sort can be used for single
digit sort. (Why?)
– So Time complexity of one iteration is Theta(N) for Counting
Sort (Why?)
• For a 3 digit number array, Time complexity become
around 3 x Theta(N) = Theta(N)
• For an array of D-digits numbers Time complexity is still
D x Theta(N) =Theta(N)
Bucket Sort
An old concept before Divide-and-Conquer.