0

The following code is supposed to sort the array initialized below.

int[] intArray = { 32, 42, 1, 23, 56, 75, 32, 23 };
int temp = 0;

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

What should you type to get the sorted array and where should you type it?

I have tried some options such as System.out.println(temp) between the last two closing brackets and the one before those, but I am not getting all the values printed, and 32 is printing many times instead.

Writing the code System.out.println(j) or System.out.println(i) in the same area doesn't work either. Can you explain why these codes don't work?

durron597
  • 31,968
  • 17
  • 99
  • 158
Andrew
  • 281
  • 3
  • 15

2 Answers2

0

If all you want to do is print the sorted array, simply print the elements in a for loop after the array is sorted:

for(int i=0; i < intArray.length; i++)
{
    System.out.println(intArray[i]);
}
JSarwer
  • 313
  • 2
  • 16
0

As your code sorts an array, if you print something inside any bracket it will show the process, not the answer. try to print the array after the last bracket.

    int[] intArray={32,42,1,23,56,75,32,23};                
    int temp = 0;

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

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

I'm not a native english speaker so I apologize if there's any grammatical error in my words.