I am reading the definitions of radix, counting and bucket sorts and it seems that all of them are just the code below:
public static void sort(int[] a, int maxV
Your code is simple variant of counting sort without data, just keys.
Radix sort is sort based on this method. The problem with counting sort is memory requirement: int [] bucket=new int[maxVal+1];
. Radix sort solves this problem. The idea is to use counting sort several times, first for lower digits, then for higher. For example, to sort 32-bit integers you might use:
sort(a, 65535) using lower half as key
sort(a, 65535) using higher half as key
It works, because counting sort is stable - it keeps order of data with equal keys. It's like sorting in spreadsheet: sort by B; sort by A
gives you elements sorted by A, and by B when As are equal.
Bucket sort is a generalization of counting sort. You can use it to sort real numbers from some predictable probability distribution (eg. uniform (0,1)
). The idea is to use counting sort (using floor(x*N_BUCKETS)
as key) and then only sort each bucket independently.