inOrder = array[j] <= array[j+1];
Yes.
The equal sign is necessary to allow repeated values.
The condition in the for
statement causes the loop to stop as soon as an out-of-order pair is found:
// scan the list starting at index 0 for ( int j=0; j<array.length-1 && inOrder; j++ ) { // check the pair starting at j inOrder = array[j] <= array[j+1]; }
This is a little bit tricky. Here is another version:
// Determine if an array is sorted in ascending order // public static boolean isSorted( int[] array ) { boolean inOrder = true; for ( int j=0; j<array.length-1; j++ ) { if ( array[j] > array[j+1] ) inOrder = false; } return inOrder; }
Is the alternate version correct?