0

Alright, so I tried implementing the bubble sort algorithm into my code, but now my output for the second array (in my code) is giving me a ton of zeros. Can anybody tell me what is wrong with my code and how I can fix it so the zeros are removed and the only thing that remains in the output for my second array are the fixed numerically?

public static void main(String[] args) {

        System.out.println("Input up to '10' numbers for current array: ");

        int[] array1 = new int[10];
        int i;

        Scanner scan = new Scanner(System.in);

        for (i = 0; i < 10; i++) {
            System.out.println("Input a number for " + (i + 1) + ": ");
            int input = scan.nextInt();
            if (input == -9000) {
                break;
            } else {
                array1[i] = input;
            }
        }

        System.out.println("\n" + "Original Array: ");

        for (int j = 0; j < i; j++) {

            System.out.println((j + 1) + ": " + array1[j]);
        }

        int[] array2 = new int[i];

        System.out.println("\n" + "Organized Array: ");

        for (int j = 0; j < i; j++) {

            int temp;
            boolean organized = false;

            while (organized == false) {
                organized = true;


            for (i = 0; i < array1.length - 1; i++) {

                if (array1[i] > array1[i + 1]) {
                    temp = array1[i + 1];
                    array1[i + 1] = array1[i];
                    array1[i] = temp;
                    organized = false;
                }
            }

            }
            for (i = 0; i < array1.length; i++) {
                System.out.println(array1[i]);
            }
            scan.close();

        }
    }
}
albusshin
  • 3,930
  • 3
  • 29
  • 57
Jeremy
  • 51
  • 7

2 Answers2

1

Copy your array1 to an array2 of the correct length before sorting, something like

for (i = 0; i < 10; i++) {
    System.out.println("Input a number for " + (i + 1) + ": ");
    int input = scan.nextInt();
    if (input == -9000) {
        break;
    }
    array1[i] = input;
}
int[] array2 = Arrays.copyOfRange(array1, 0, i);
System.out.println("Before sorting: " + Arrays.toString(array2));
Arrays.sort(array2); // <-- How I would sort.
System.out.println("After sorting: " + Arrays.toString(array2));

The reason this is necessary is because i might not be 10 in which case your array contains 0(s) to fill the other positions.

Is it possible to move all my numbers from Array 1 to Array 2 using a for-loop?

Yes. You could implement a copyOfRange function with a for loop,

private static int[] copyOfRange(int[] arr, int start, int end) {
    int pos = 0;
    int[] out = new int[end - start];
    for (int i = start; i < end; i++) {
        out[pos] = arr[i];
        pos++;
    }
    return out;
}

the built-in version is almost certainly better.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Your answer is technically correct (+1) but the OP really wanted to manually implement bubble sort. – C. K. Young Mar 11 '15 at 05:02
  • @ChrisJester-Young In which case OP can, by sorting `array2` without using `Arrays.sort(int[])` – Elliott Frisch Mar 11 '15 at 05:03
  • Is it possible to move all my numbers from Array 1 to Array 2 using a for-loop? I kind of want to get the basics down and understand them first before using codes such as `Arrays.copyOfRange()`. Edit: I mean, they are there in the second array, but it says that my `int[] array2 = new int[i];` isn't being used... and underlined yellow. Sorry, I'm still a newbie at using Java. – Jeremy Mar 11 '15 at 05:06
  • @ElliottFrisch, I'm sorry... I haven't learned how to build classes yet. I'm planning on learning that sometime in the future. Would that code work the same without classes? – Jeremy Mar 11 '15 at 05:14
  • @Jeremy all Java programs have classes. – Dawood ibn Kareem Mar 11 '15 at 05:24
  • Uhm, I meant. Having different classes within the code and calling out a piece of code from within the function to another area of the code... if that makes any sense. Sorry if I'm confusing. – Jeremy Mar 11 '15 at 05:26
1

1) You are printing the array multiple times, I think you might be giving 0 as input and thats the reason you are seeing 0's everywhere.

2) You have created array2 which is not necessary.

Move the printing logic out of for loop as in the below snippet. Otherwise your logic looks fine except fot the wrong looping of print statement.

public static void main(String args[]) {
        System.out.println("Input up to '10' numbers for current array: ");
        int[] array1 = new int[10];
        int i;
        Scanner scan = new Scanner(System.in);
        for (i = 0; i < 10; i++) {
            System.out.println("Input a number for " + (i + 1) + ": ");
            int input = scan.nextInt();
            if (input == -9000) {
                break;
            } else {
                array1[i] = input;
            }
        }
        System.out.println("\n" + "Original Array: ");
        for (int j = 0; j < i; j++) {
            System.out.println((j + 1) + ": " + array1[j]);
        }
        int[] array2 = new int[i];
        System.out.println("\n" + "Organized Array: ");
        for (int j = 0; j < i; j++) {
            int temp;
            boolean organized = false;
            while (organized == false) {
                organized = true;
                for (i = 0; i < array1.length - 1; i++) {
                    if (array1[i] > array1[i + 1]) {
                        temp = array1[i + 1];
                        array1[i + 1] = array1[i];
                        array1[i] = temp;
                        organized = false;
                    }
                }
            }
            scan.close();
        }
        for (i = 0; i < array1.length; i++) {
            System.out.println(array1[i]);
        }
    }
kondu
  • 410
  • 3
  • 11
  • Hi @Kondu, I don't have 0s as input. But I tried running the modified code you gave me and I still have 0s as my output. What could be wrong? – Jeremy Mar 11 '15 at 05:43
  • @Jeremy This is the output I see when I run the code **Input up to '10' numbers for current array: Input a number for 1: 1 Input a number for 2: 2 Input a number for 3: 3 Input a number for 4: 4 Input a number for 5: 55 Input a number for 6: 6 Input a number for 7: 77 Input a number for 8: 8 Input a number for 9: 99 Input a number for 10: 10 Original Array: 1: 1 2: 2 3: 3 4: 4 5: 55 6: 6 7: 77 8: 8 9: 99 10: 10 Organized Array: 1 2 3 4 6 8 10 55 77 99** Please specify how are you executing this? – kondu Mar 11 '15 at 06:55
  • Try running it without all 10 numbers. I want the user to enter as many numbers as they want up to 10... and if there are less than 10 numbers entered, "-9000" gives them the results. – Jeremy Mar 11 '15 at 06:59