I'll take these data as an example:
vetor = 71,72,73,74,75,76,77
n = 7 (que é o tamanho de "vetor")
So the loop will cause i
to go from 0
and stop when it arrives at 7/2
, which is 3
(how i < n/2
was used 3
will not be iterated).
Based on this, see each of the iterations in action, already changing n
by 7
, in [n-1-i]
and applying i
.
These instructions will be executed 3 times, with i
being 0
, 1
and 2
:
aux = vetor[i]; vetor[i] = vetor[7-1-i]; vetor[7-1-i] = aux;
Let's apply the loop, with i
and [7-1-i]
of the 3 iterations in the operations:
i = 0 portanto 7-1-i = 6 -> aux = vetor[0]; vetor[0] = vetor[6]; vetor[6] = aux;
i = 1 portanto 7-1-i = 5 -> aux = vetor[1]; vetor[1] = vetor[5]; vetor[5] = aux;
i = 2 portanto 7-1-i = 4 -> aux = vetor[2]; vetor[2] = vetor[4]; vetor[4] = aux;
Let's replace the right side of the assignments with their values:
i = 0 portanto 7-1-i = 6 -> aux = 71; vetor[0] = 77; vetor[6] = 71;
i = 1 portanto 7-1-i = 5 -> aux = 72; vetor[1] = 76; vetor[5] = 72;
i = 2 portanto 7-1-i = 4 -> aux = 73; vetor[2] = 75; vetor[4] = 73;
So, after executing the 3 steps of the loop:
vetor[0] = 77
vetor[1] = 76
vetor[2] = 75
vetor[3] = 74 (isso nao mudou)
vetor[4] = 73
vetor[5] = 72
vetor[6] = 71