Skip to content

Instantly share code, notes, and snippets.

@kuzemkon
Created June 13, 2016 14:19
Show Gist options
  • Save kuzemkon/8c0e2740c2782339795bd171cbe01182 to your computer and use it in GitHub Desktop.
Save kuzemkon/8c0e2740c2782339795bd171cbe01182 to your computer and use it in GitHub Desktop.
Radix Sorting
public class Radix {
private int[] array = new int[11];
public Radix(int[] array){
sort(array);
}
private void sort( int[] a)
{
int i, m = a[0], exp = 1, n = a.length;
// int[] b = new int[10];
for (i = 1; i < n; i++)
if (a[i] > m)
m = a[i];
while (m / exp > 0)
{
int[] bucket = new int[10];
for (i = 0; i < n; i++)
bucket[(a[i] / exp) % 10]++;
for (i = 1; i < 10; i++)
bucket[i] += bucket[i - 1];
for (i = n - 1; i >= 0; i--)
array[--bucket[(a[i] / exp) % 10]] = a[i];
for (i = 0; i < n; i++)
a[i] = array[i];
exp *= 10;
}
}
public int[] getResult(){
return this.array;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment