Bucket sort is the simplest sorting algorithm, And it have O(n) tine complexity
Suppose we have an array A of N elements.
Lets say each element is in range of 0 < a[i] <100
Then we simply take an array B of 100 elements.
And move through the array A, for each array element just increment the counter of that position in array B
For e.g.:
if A = [5,4,2,2,3]
B = [0,0,0,0,0]
After moving through each element of A
B would become [0,0,2,1,1,1]
which indicated we have two 2's one 3,4 and 5;
so we can simply give the sorted elements = 2,2,3,4,5.
No comments:
Post a Comment