I am fairly new to programming so I made an array sorter using the radix method.
I can print out my sorted array every time when I do not have the unsorted array print statement (I have tried moving the print statement within the main method to no avail).
It works with some numbers however, 2 and 5 will fail. Others will usually work perfectly fine.
I would like to say that while I am new to programming this is NOT a homework assignment and just something for personal benefit. I would prefer to get answers that challenge me to think rather than functional code.
Thank you all for your patience. I have asked professors and TA's to no avail so this is really a last resort.
Here is my code. The console prints the "Unsorted Array" but no numbers so there error is somewhere within my for loop but nothing logically make sense.
import java.util.Scanner;
public class Sort {
public static void Sort(int[] a, int n) {
int i, importArray = a[0], denominator = 1, arrayLength = a.length;
System.out.println("\nUnsorted Array");
for (i = 0; i < arrayLength; i++) {
if (i + 1 != arrayLength) {
System.out.print(a[i] + ":");
} else {
System.out.print(a[i]);
}
}
int[] sorter = new int[n];
for (i = 1; i < arrayLength; i++) {
if (a[i] > importArray) {
importArray = a[i];
while (importArray / denominator > 0) {
int[] bucket = new int[n];
for (i = 0; i < arrayLength; i++) {
bucket[(a[i] / denominator) % 10]++;
}
for (i = 1; i < n; i++) {
bucket[i] += bucket[i - 1];
}
for (i = arrayLength - 1; i >= 0; i--) {
sorter[--bucket[(a[i] / denominator) % 10]] = a[i];
}
for (i = 0; i < arrayLength; i++) {
a[i] = sorter[i];
}
denominator *= 10;
}
}
}
}
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.println("Radix Array Sorting");
int n, i;
System.out.println("Enter Number of Array Elements");
n = userInput.nextInt();
int array[] = new int[n];
System.out.println("Enter " + n + " Elements For Array");
for (i = 0; i < n; i++) {
array[i] = userInput.nextInt();
}
Sort(array, n);
System.out.println("\nElements After Sorting");
for (i = 0; i < n; i++) {
if (i + 1 != n) {
System.out.print(array[i] + ":");
} else {
System.out.print(array[i]);
}
}
}
}