//this function verifies if an integer array is sorted in a range
//returns false if low and high are not in the range: [0-length[ and low >= high
//returns false if numbers[i] > numbers[i+1]: if the actual number is higher than the next one
//otherwise returns true
bool isSorted( constint* numbers, constint length, constint low, constint high )
{
if ( low >= 0 && high < length && low < high ) {
//if the range is composed by just 2 numbers
if ( high - low == 1 ) {
if ( numbers[low] > numbers[high] )
returnfalse;
} else {
for ( int i = low; i< high - 1; i++ )//EDIT: ERRROOOOORRRR!!!!
if ( numbers[i] > numbers[i + 1] )
returnfalse;
}
} elsereturnfalse;
returntrue;
}
You do not need to actually pass so many parameters.
It is enough to pass pointer to the first and to the last (or one-past-the-last to be concise with standard library) element.
Your code checking only current and last valid elements. You should do something similar to:
1 2 3 4 5 6 7 8 9 10
bool isSorted( constint* first, constint* last)
{
if (first == last)
returntrue;
while (++first != last) {
if (*first > *(first - 1))
returnfalse;
}
returntrue;
}
doesn't control if the pointers are pointing to the elements of the array
And it should not. Your prevous code does not check if array pointer indeed points to the array and if length is valid either.
And after implementing two-pointers variety of isSorted you might be surprised to know that there is standard function is_sorted which works not only on arrays, but on basically anything which supports iterators (raw pointers can be seen as raw arrays iterators)
Oh yes, sorry.
Please note that your code is going to loop infinitely if first == one_past_the_last. It can happen when range is calculated by different functions (empty range is still a range and, therefore, valid). So replace != on line 3 with < (I left early return when translating from forward iterator based to pointer based function)
And first parameter should be constint* too.
constint* x; // or int const * x means that x is a mutable pointer to constant element, i.e. you can change pointer itself, but not the value it points to:
1 2
x += 1;//legal
*x = 1;//illegal
int* const x; means that x is constant pointer to mutable element. You cannot make pointer poiint to another variable, but you can change value it points to:
1 2
x += 1;//illegal
*x = 1;//legal
And finally constint* const x; // or int const * const x means that x is a constant pointer to constant element. You cannot change anything. Only read value it points to.
Is it legal to check a range of one element? It seems to me that a collection of 1 is always sorted. Currently your code says that it is always unsorted.
It seems to me that a collection of 1 is always sorted
Yes it is. As is collection of 0 elements
Currently your code says that it is always unsorted.
Nope, while( ++first != one_past_the_last ) (or more correctly while( ++first < one_past_the_last )) will evaluate to false right away, making function to return true.
@naraku9333 (1951)
What do you mean? The correct function verifies if a range of an array is sorted [low-high], not from 0, the first element of the array.