I have an array as such:
int array[] = { 1,3,2,5,4,7,6,9,8,10 };
When I try to step through the array and compare the numbers none of them trigger the if condition thereby triggering the swap:
for( int i=0; i<9; i++)
{
if (array[i] > array[i++])
{
cout << "Swapping" << array[i] << " " << array[i++]<< endl;
int temp = 0;
temp = array[i];
array[i] = array[i++];
array[i++] = temp;
temp = 0;
}
}
Is there some detail of comparing integers that I am missing? Are they treated differently because they are in an array?
i++means “returniand seti = i + 1“. So each time you’re usingi++you are increasingiby one which ruins the loop. usei+1instead.