简体   繁体   中英

Java - Array Bubble Sorting

1  |  int[] numbers = { 5, 8, 14, 1, 5678 };
2  |  int tempVar;
3  |  for (int i = 0; i < numbers.length; i++)
4  |   {
5  |       for(int j = 0; j < numbers.length; j++)
6  |       {
7  |                if(numbers[i] > numbers[j + 1])
8  |                {
9  |                        tempVar = numbers [j + 1];
10 |                        numbers [j + 1]= numbers [i];
11 |                        numbers [i] = tempVar;
12 |                 }
13 |        }
14 |  }
15 |  for (int i = 0; i < numbers.length; i++)
16 |  {
17 |           System.out.println(numbers[i].toString());
18 |  }

I am a newbie to Java who learn on my own. I have some problems with bubble sorting.

My problems are with line 5 to 7 . From my understanding, the outer loop starts with i is 0 , then the inner loop runs from j is 0 and increases by 1 each time until j reaches 4 , after which the outer loop will advance to i is 1 . Thus before the outer loop advances to i is 1 , the following should happen.

When i=0 , number i=5 Then the inner loop runs:

When j=0 , number j=5

When j=1 , number j=8

When j=2 , number j=14

When j=3 , number j=1

When j=4 ,number j=5678

Then if numbers i is greater than numbers j+1 , the two numbers are swapped.

Then we are comparing 5 with 5 , then 5 with 8 , then 5 with 14 , then 5 with 1 , and then 5 with 5678 , which is different from how bubble sort works (which compares 5 with 8 , then 8 with 14 , then 14 with 1 and then 1 with 5678 ).

I can't understand how the codes in line 5 to 7 can result in comparing the two neighboring figures in the way supposed to be in bubble sort. Can anyone point out what I have thought wrong?

It would be grateful if any one points out, in greater detail, how lines 5 to 7 work. It would be even better if a step by step breakdown could be provided. Thanks!

Watch this from around 35.00 and thats bubble sort. Your understanding is correct. Bubble sort compares elements 1, 2 - and if elem 1 > elem 2 then swaps them - then 2,3 and then 3,4 and so on until n-1 and n. In the first pass the largest element is element n. So in the second pass you need not check for the last element and you need to go only until n-1 th element.

For each pass you use index 'j' and for over all passes you use index 'i'.

So you dont use index 'i' during a pass for swapping.

So in your 5th and seventh line

5  |       for(int j = 0; j < numbers.length; j++)
6  |       {
7  |                if(numbers[i] > numbers[j + 1])

Instead of using index 'i' for comparison use do this

7  |                if(numbers[j-1] > numbers[j])

starting j from 1.

And as I said after each pass using index 'j' you need to go one element less the next time as each passes places the largest element at the last position for that pass. So you need not bother about the last element after a pass. So your line five becomes

5  |       for(int j = 1; j < numbers.length-i; j++)

as after each pass 'j' goes only upto one element less.

So the over all loop

for(i=0 to n)
{   
   for(j=1 to n-i)
   {
       if(array[j-1]>array[j])
           swap(array[j-1],array[j]);
   }  
}

You need to do like this with your inner and outer loops:

for (int i = 0; i < numbers.length-1; i++)
{
  for(int j = i+1; j < numbers.length; j++)
   {
     if(numbers[i] > numbers[j])
     {
      tempVar = numbers [i];
      numbers [i]= numbers [j];
      numbers [j] = tempVar;
     }
   }
}

Well, there are multiple implementations of bubble sort, one is suggested by Kinar. In your case you are just picking up the indices one by one in line 5 that is:

    5  |       for(int j = 0; j < numbers.length; j++)

it iterates the whole array and in line 7 you are comparing the jth element of array with the ith element that is:

    7  |                if(numbers[i] > numbers[j + 1])

When i=0, number i=5 then the inner loop runs and it compares 5 with all the elements of array from 1 to the nth index. If the ith element is greater than number[j+1] then it will be swapped with that number to make the numbers in ascending order. Remember, at the end of each iteration there must be at least one element which is placed to the right place according to the sorting order. So at the end of first ith iteration the array will look like:

    numbers = { 1, 8, 14, 5, 5678 };

now consider i=1, number i=8 then again the inner loop runs and it compares 8 with all the elements from 1 to the nth index. If the ith element is greater than number[j+1] then it will be swapped with that number.

Another important tip is that line 5 should be:

    5  |       for(int j = 0; j < numbers.length - 1; j++)

Because in line 7 you are accessing numbers[j + 1], when value of j will be 4, program will try to access numbers[5] in the if statement of line 7 that will cause an error. Hope this helps.

Basically you described correctly what the line 5 to 7 do.

then the inner loop runs from j is 0 and increases by 1 each time until j reaches 4

This is also right, since numbers.length is 5.

numbers[j + 1]

This cannot be right. If j is 4, then j+1 is 5 and numbers[5] will throw an index out of bound exception (or something simmilar).

You need to do like this:

public static void bubbleSort(int[] numArray) {

    int n = numArray.length;
    int temp = 0;

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

            if (numArray[j - 1] > numArray[j]) {
                temp = numArray[j - 1];
                numArray[j - 1] = numArray[j];
                numArray[j] = temp;
            }

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM