Suppose list L contains n numbers that are known to be in the range [0, 2 n]. Design an algorithm for sorting L in linear time.
I'm not too sure how to solve this. I believe radix sort is O(dn).
Suppose list L contains n numbers that are known to be in the range [0, 2 n]. Design an algorithm for sorting L in linear time.
I'm not too sure how to solve this. I believe radix sort is O(dn).
Looks like homework. What's about this algorithm in some pseudo code:
2n
x
in list, store flag[x] = true
2n
print i
if flag[i]
is true.All iteration loops are bound by O(n).
Something like the following:
2n
initialised with 0 values.L
, increment arr[L_i]
.This algorithm can be easily modified to handle non-distinct values in L
by modifying step 3 slightly.