Question

I am trying to create a bubble sort program for a small presentation that I am doing. The code runs fine but the largest integer in my array is never displayed. Output for the program as it stands is:

1
3
4
6
7
11

The 12 is missing!

Here is my code, excuse the comments - some of them might even be wrong.

public class BubbleSort {

public static void main(String[] args) {

    int number []={6,3,1,7,4,12,11};
                // 0,1,2,3,4

    //For temporarily storing a value that has to be swapped
    int temp;

    //Keeps the loop running until there is nothing left to sort.
    boolean fixed=false;

    while (fixed==false) {

    fixed=true;

    //If this IF statement is accessed it means something still has to be
    //swapped, so at the end of the statement fixed is reverted to false
    //again, so it can continue the loop.


    for(int i=0; i<number.length-1 ; i++){  
        //This makes i start at 0 the first time it is run,
        //this is due to the array starting at 0, too.


        if(number[i] > number[i+1]) {
            //If 8   >   5

            temp = number[i+1];
            //Store 5 in temporary variable

            number[i+1]=number[i];
            //Swap array 1 with array 0/

            number[i]=temp;

            fixed=false;

        }


    }


    }

    for(int i=0; i<number.length-1 ; i++){
        System.out.println(number[i]);
    }

}

}
Was it helpful?

Solution

for(int i=0; i<number.length-1; i++){
    System.out.println(number[i]);
}

Should be

//                            no '-1'
//                              V
for(int i = 0; i < number.length; i++) {
    System.out.println(number[i]);
}

Why?

Let's see what happens if there are 5 elements in the array, then they are indexed as follows:

0, 1, 2, 3, 4

If we use i < 5-1 (or i < 4), once i becomes 4, before the next iteration of the loop runs, it will stop, skipping the last index.

In case the above doesn't explain it, note the order in which things happen in a for-loop:

  • First the initialization occurs
  • Then the following is repeated until the condition is false:
    • The condition is checked
    • A loop iteration is run
    • The increment happens

The most applicable part here is that a loop iteration will never run if the condition is false.

OTHER TIPS

The error is in your last loop showing numbers.

Your loop does not reach the last element because it stops in number.length-2. You have to add one more execution to your loop.

Try:

for(int i=0; i<number.length ; i++){
        System.out.println(number[i]);
    }

or

for(int i=0; i<=number.length-1 ; i++){
            System.out.println(number[i]);
        }
for(int i=0; i<number.length-1 ; i++){
    System.out.println(number[i]);

When you are printing your array, remove -1 only write number.length.

you are using wrong ending loop condition it should be

       for(int i=0;i<number.length;i++)

as array index starts from 0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top