Note:
I already asked a specific question on this programm before, but now I'm stuck at the very last step and I guess it might be better to open a new thread for it.
Description:
I'm required to implement a programm that sorts numbers ranging from 0 to 99999 recursively (this is basically Radix sort). The process itself is kinda simpel: The user types in an array that contains those numbers in the main method. Then, the main method calls for the sort-method where I create a two-dimensional array named 'space' with 10 rows and 1 column. Then, I divide every number in the array by the digit, which would be 10.000 in the first run. So, for example, 23456 / 10000 = 2,3456 = 2 (in java), hence, the programm puts this number in space[2][0], so in the second row. Then, we take this entire row and extend it, which is done in the putInBucket-method. We do this in order to make sure that we can put another number into the same row.
We do this for every number that is inside the 'numbers'-array. Then, we want to work with these rows and sort them again by the same principle, but now we take a look at the second digit. We want to do this from left to right, not from right to left. So, if our second row would look like this
[23456, 24567],
we'd want to compare the 3 and the 4. In order to do so, we calculate digit / 10 in each recursive call. If the digit is 0, there is nothing to sort anymore.
The recursive call itself works with the rows from 0 to 9 where we put in the different numbers before and now sorts them again by putting them in different rows.
Question:
I think the programm does what it is supposed to do. Unfortunately, I don't know how to print the result properly. For example, in the code below, I tried to print out the bucket in the main method, but it only gives me exactly the array that I just typed in, so that can't be right.
I need to start with all the elements in the row 9, and if this row contains more than one number, I'd have to sort them by the results I got in the recursive call.
Does anyone have an idea how to implement this properly? Thanks in advance!
public static int[] sort(int[] numbers, int digit) {
if (numbers.length <= 1 || digits == 0)
return numbers;
int[][]space = new int[10][1];
int i, j = 0;
for (j = 0; j < numbers.length; j++) {
i = numbers[j] / digit % 10;
space[i][0] = numbers[j];
space[i] = putInBucket(space[i], numbers[j]);
}
digit = digit / 10;
for (i = 0; i < 9; i++) {
sort(space[i], digit);
}
return numbers
}
private static int[] putInBucket(int[] bucket, int number) {
int[] bucket_new = new int[bucket.length+1];
for (int i = 1; i < bucket_new.length; i++) {
bucket_new[i] = bucket[i-1];
}
return bucket_new;
}
public static void main (String [] argv) {
int[] numbers = IO.readInts("Numbers: ");
int digit = 10000;
int[] bucket = sort(numbers, digit);
for (int i = 0; i < bucket.length; i++) {
System.out.println(bucket[i]);
}
As I demonstrated in my answer to your previous question, you need to return the sorted
numbers
. You get the sorted numbers by going throughspace
from the smallest to the largest digits.For that, you would probably need to keep track of the lengths of the buckets for each digit (the
len[i]
variable).The full solution would be: