An array contains integers that first increase in value and then decrease in value. It is unknown at which point the numbers start to decrease. Write efficient code to copy the numbers in the first array to another array so that the second array is sorted in ascending order.
The code is below:
int _tmain(int argc, _TCHAR* argv[])
{
int a[10] = { 17, 24, 31, 39, 44, 49, 36, 29, 20, 18 };
int b[10];
int i = 0, j = 9, k = 0;
while (i <= j) {
if (a[i] < a[j]) {
b[k++] = a[i++];
}
else {
b[k++] = a[j--];
}
}
return 0;
}
What is the loop invariant here?