Hello.

Tell me, please, why the sorting method does not work correctly, the figure 5 disappears somewhere. Everything works in the Pascal language (the code translated with maximum accuracy).

 class test { protected static int a, b; protected static int[] array = { 2, 21, 42, 43, 567, 5 }; protected static final int ARRAY_LENGTH = array.length; public static void main(String[] args) { for(int i = 0; i < ARRAY_LENGTH - 1; i++) { for(int j = 0; j < ARRAY_LENGTH - 2; j++) { a = array[j]; b = array[j + 1]; if(a > b) { array[j] = b; array[j + 1] = a; } } } for(int i = 0; i < array.length - 1; i++) { System.out.print(array[i] + " "); } } } 

    4 answers 4

    BP internal cycle

      for(int i = 0; i < ARRAY_LENGTH - 1; i++) { for(int j = 0; j < ARRAY_LENGTH - 2; j++) { a = array[j]; b = array[j + 1]; if(a > b) { array[j] = b; array[j + 1] = a; } } } 

    The maximum index of an element that can be accessed is ARRAY_LENGTH - 2 . Therefore, the last element of the array remains "untouchable" :)

    Imagine that the size of the array is 2. In this case, ARRAY_LENGTH - 2 will be 0. As a result, the inner loop will never be executed, and the array of two elements will not be sorted.

    And besides, in this loop, you also incorrectly specified the upper limit of the index, so, again, the last element of the array is not displayed.

      for(int i = 0; i < array.length - 1; i++) { ^^^^^^^^^^^^^^^^^ System.out.print(array[i] + " "); 

    And if you enter the ARRAY_LENGTH constant, then use it everywhere instead of the member of the length class. Must be

      for(int i = 0; i < ARRAY_LENGTH; i++) { ^^^^^^^^^^^^ System.out.print(array[i] + " "); 

      The last element of the array is lost here.

       for(int i = 0; i < array.length - 1; i++) 

      and here

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

      Why do you subtract one from the length if you compare it with the operator β€œstrictly less”?

      • I do not miss it, because the array counter starts from zero, i.e. if the length of the array is 2, and I need to get the last element (the second), I need to subtract one from the length (2 - 1 = 1). - ikerya
      • @ikerya, Only here in the condition you have strictly more. If you subtract one, the condition must be greater than or equal to, and not strictly greater. - post_zeew pm
      • @ikerya is just your cycle, on two elements it will be executed only once - for i = 0, because at the next iteration you have i = 1, and the exit conditions i <(2-1) => i <1 are rjhdby

      First, in the inner loop, you do not compare the last two elements of the array, since j < ARRAY_LENGTH - 2 .

      Secondly, when displaying the elements of an array on the screen, you output all the elements, except for the last one, since you have i < array.length - 1 .

        Here is the whole logic of your application.

         int[] arr = new int[]{90,123,12,0,1,4,7,44,-2,-4,-5}; int a = 0; int max = -50; int imax = -50; max = arr[0]; for (int i = 0;i<arr.length;i++){//ВыполняСтся ΠΏΠΎΠΊΠ° Π½Π΅ ΠΊΠΎΠ½Ρ‡Π°Ρ‚ΡŒΡΡ элСмСнты массива. for(a = i; a<arr.length;a++){//Находит ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ элСмСнт max = -50; if(max<arr[a]) { max = arr[a];//ΠœΠ΅Π½ΡΠ΅Ρ‚ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ элСмСнт imax = a;//Π—Π°ΠΏΠΎΠΌΠΈΠ½Π°Π΅Ρ‚ ΠΊΠ°ΠΊΠΎΠΉ элСмСнт массива ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ } if(arr[i]<max){//Π‘Ρ€Π°Π²Π½ΠΈΠ²Π°Π΅ΠΌ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ элСмСнт с Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΌ Ссли ΠΎΠ½ мСньшС Ρ‚ΠΎ замСняСм значСния arr[imax]=arr[i];//ΠœΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ элСмСнт = Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΌΡƒ arr[i] = max;//Π’Π΅ΠΊΡƒΡ‰ΠΈΠΉ Ρ€Π°Π²Π΅Π½ ΠΌΠ°ΠΊΡΠΌΠΈΠΌΠ°Π»ΡŒΠ½ΠΎΠΌΡƒ } } } for (int ac = 0; ac<arr.length;ac++){ System.out.println(arr[ac]+" "); } } 
        • You, probably, misunderstood me, I don’t need someone else’s code (the more so after killing if I understood correctly). I have somewhere in the top five - ikerya
        • Since this is the last element, you skip it in this code: for (int i = 0; i <ARRAY_LENGTH - 1; i ++) - Romag