0% found this document useful (0 votes)
18 views

Radix Sort

Radix sort is an algorithm that sorts strings by their characters from the highest to lowest position. It uses counting sort as an inner stable sorting method. The algorithm iterates through each character position of the strings, sorting them based on that character using counting sort, which counts the number of occurrences of each character and uses that to place the strings in sorted order. This process is repeated for each character position from highest to lowest to fully sort the strings from lowest to highest.

Uploaded by

RizkiAlifian
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Radix Sort

Radix sort is an algorithm that sorts strings by their characters from the highest to lowest position. It uses counting sort as an inner stable sorting method. The algorithm iterates through each character position of the strings, sorting them based on that character using counting sort, which counts the number of occurrences of each character and uses that to place the strings in sorted order. This process is repeated for each character position from highest to lowest to fully sort the strings from lowest to highest.

Uploaded by

RizkiAlifian
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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

You might also like