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 new to Java that learns by myself. I have some problems with sorting bubbles.

My problems are with the line 5up 7. In my opinion, the outer loop starts with iis 0, then the inner loop runs from jis 0and increases 1each time until jit reaches 4, after which the outer loop will advance to iis 1. Thus, before the outer loop reaches i,   1should happen next.

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

When j=0, numberj=5

When j=1, the numberj=8

When j=2, numberj=14

When j=3, the numberj=1

When j=4, numberj=5678

, , j+1,  .

5 5, 5 8, 5 14, 5   1, 5 5678, ,  ( 5 8, 8 14, 14 1   1 5678).

, 5 7   , ,   . - , ?

, - ,   5 7 . ,   ​​. !

+4
5

, , . 5, :

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

, 7 j- i- , :

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

= 0, = 5, , 5 1 n- . i- [j + 1], , . , , . :

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

= 1, = 8, , 8 1 n- . i- [j + 1], .

, 5 :

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

7 [j + 1], j 4, [5] if 7, . , .

+1

35.00, . . Bubble 1, 2 - elem 1 > elem 2 - 2,3 3,4 n-1 n. n. , n-1- .

"j", "i".

, "i" .

, 5-

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

'i'

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

1.

, 'j', , . . ,

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

'j' .

,

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

:

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;
     }
   }
}
+2

, , 5 7.

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

, numbers.length 5.

numbers[j + 1]

It is not right. If j is 4, then j + 1 is 5, and the numbers [5] will cause an index from the associated exception (or something similar).

0
source

You need to do the following:

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;
            }

        }
    }
}
0
source

All Articles