Radix Code (Data Structures)
Radix Code (Data Structures)
import java.util.LinkedList;
import java.util.Queue;
/**
* Implementation of radix sort.
*
* @author Anton Gustafsson
*
*/
public class Radix {
/**
* Array with numbers to be sorted.
*/
public Radix() {
int[] unsorted1 = { 9, 179, 139, 38, 10, 5, 36 };
int[] unsorted2 = { 667342, 1745359, 556139, 343538, 5510, 45, 5536 };
radixSort(unsorted1);
}
/**
* Performs radix sort, a kind of bucket sort. From right to left, take the
* least significant number and put them in their corresponding bucket. Then
* take them out, and that value is now sorted. Then do it again for all
* valules, ex first "ones", then "tens" and so on.
*
* @param unsorted
*/
public void radixSort(int[] unsorted) {
// Create a box with 10 buckets.
Queue[] box = new Queue[10];
for (int i = 0; i < box.length; i++) {
box[i] = new LinkedList<Integer>();
}
// Now remove the numbers in order and put them in a new array.
int x = 0;
for (int j = 0; j < box.length; j++) {
while (!box[j].isEmpty()) {
unsorted[x] = (int) box[j].remove();
x++;
}
}
System.out.print("Partly Sorted " + n + "'s: (");
printArray(unsorted);
m *= 10;
n *= 10;
}
System.out.print("Sorting complete: ");
printArray(unsorted);
}
/**
* Prints the given arrays elements.
*
* @param arr
*/
private void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + ", ");
}
System.out.println(")");
}
}