I am implementing radix sort using queues. As of now, it works for sorting integers, but I want to modify it to sort positive floating points as well.
public static void radixSort(int[] array) {
int max = getMaxElement(array);
List<Queue<Integer>> queues = new ArrayList<>(10);
for (int i = 0; i < 10; i++)
queues.add(new LinkedList<>());
/* insert into queues based on radix */
for (int exp = 1; max / exp > 1; exp *= 10) {
for (int value: array) {
int digit = value / exp % 10;
queues.get(digit).add(value);
}
}
/* dequeue into original array */
int index = 0;
for (int i = 0; i < 10; i++) {
while (!queue.get(i).isEmpty())
array[index++] = queues.get(i).remove();
}
}
I tried by finding the longest decimal places then multiplying each element in the array by 10 raised to the power of it so that the floating points can be sorted as integers. However, the method I used would require converting them to strings to find the .
longest decimal places. Is there a more elegant way for sorting floating points?