0

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]);
        }
    }
}
}
  • 2
    Just skimming your code, but you are reusing 'i' in your inner loops. This will mess with the 'i' in your outer loop. Use 'j', 'k', etc. for each level of nesting – Briguy37 Nov 02 '16 at 14:24
  • Eli.) The link you sent their main problem was using <= instead of < for their loops. – ncarr995 Nov 02 '16 at 15:02
  • 1
    @ncarr995 No. Main problem was in access array index bigger than array length - 1. – talex Nov 02 '16 at 15:03
  • @Briguy37 Correct me if I am wrong because I may be. These loops are not nested so the i iterator are not nested the use of "i" shouldn't cause an issue. if they were for { for { }} then yes, but since it is for {} for {} there shouldn't be one. – ncarr995 Nov 02 '16 at 15:03
  • @talex wouldn't that technically be the same thing since i < array.length and i <= array.length - 1 are basically the same thing with the -1 being the offset. – ncarr995 Nov 02 '16 at 15:07
  • 1
    @ncarr995 no. because root cause of error is not condition in loop. It is access operation itself. – talex Nov 02 '16 at 15:31
  • @ncarr995 You are right that you can re-use `i` for the main 2 `for` loops. However, the 4 `for` loops embedded in the 2nd one look problematic. – Briguy37 Nov 02 '16 at 17:46

0 Answers0