go to previous page   go to home page   go to next page
inOrder = array[j] <= array[j+1];      

Answer:

Yes.

The equal sign is necessary to allow repeated values.


Another Version

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

QUESTION 7:

Is the alternate version correct?


go to previous page   go to home page   go to next page