Created
June 13, 2016 14:19
-
-
Save kuzemkon/8c0e2740c2782339795bd171cbe01182 to your computer and use it in GitHub Desktop.
Radix Sorting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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