bool isSorted(int list[] ,int n)
{
bool done = false;
int index = 0;
while ( !done && ( index < n - 1) )
{
if (list[index] > list[index + 1]) // if not sorted...
{
done = true;
}
index++;
}
// done is TRUE if list is not sorted, and FALSE if sorted.
return !done;
}
This solution should work for any n, because it compares from (0 and 1) to (n-2 and n-1)
This solution should work for any array of numbers as it compares two neighboring elements regardless of what is stored in them
This solution should work for any permutation of those numbers. If a number is greater than its successor then FALSE is returned, if each number is smaller than its successor then TRUE is returned
This Program will terminate in n time as after size-1 iterations the while loop will terminate
No comments:
Post a Comment