I have this peace of code in wiki page about sorting. I suppose it has to swap to array elements, but can somebody explain it properly
array[i] ^= array[i-1];
array[i-1] ^= array[i];
array[i] ^= array[i-1];
I have this peace of code in wiki page about sorting. I suppose it has to swap to array elements, but can somebody explain it properly
array[i] ^= array[i-1];
array[i-1] ^= array[i];
array[i] ^= array[i-1];
That is a way of swapping array[i] and array[i-1].
^= does a bitwise XOR between the two arguments and assigns it to the left one.
To see how that causes a swap lets consider just a pair of bits : A and B which contain values x and y respectively .
As you can see A and B have been swapped.