You are on page 1of 1

# 1 of 1

http://www.programming-algorithms.net/article/40868/Radix-sort

view source
print?
01./**
02. * Radix sort (ascending)
03.

* Inner stable sort: counting sort

04.

* @param array array to be sorted

05.

* @param dimension fixed length of sorted strings

06.

* @return sorted array

07.

*/

08.public static String[] radixSort(String[] array, int dimension){
09.

for(int i = dimension - 1; i >= 0 ; i--){

10.
array = countingSortForRadix(array, i); //order strings by
characters at their i-th position
11.
}
12.
return array;
13.}
14./**
15. * Counting sort for radix sort
16.

* @param array array to be sorted

17.

* @param position position (key) used for the sorting

18.

* @return sorted array

19.

*/

20.public static String[] countingSortForRadix(String[] array, int position){
21.

String[] aux = new String[array.length];

22.
23.

char min = array[0].charAt(position);

24.

char max = array[0].charAt(position);

25.

for(int i = 1; i < array.length; i++){

26.

if(array[i].charAt(position) < min) min = array[i].charAt(position);

27.
else if(array[i].charAt(position) > max) max =
array[i].charAt(position);
28.
}
29.
30.

int[] counts = new int[max - min + 1];

31.
32.

for(int i = 0;

33.
34.

i < array.length; i++){

counts[array[i].charAt(position) - min]++;
}

35.
36.

counts[0]--;

37.

for(int i = 1; i < counts.length; i++){

38.
39.

counts[i] = counts[i] + counts[i-1];
}

40.
41.

for(int i = array.length - 1; i >=0; i--){

42.
43.

aux[counts[array[i].charAt(position) - min]--] = array[i];
}

44.
45.
46.}

return aux;

07/03/2016 10:21