tags:

views:

79

answers:

4
+2  Q: 

swaping inplace

how to swap two numbers inplace without using any additional space?

+4  A: 

The xor trick is the standard answer:

int x, y;
x ^= y;
y ^= x;
x ^= y;

xoring is considerably less clear than just using a temp, though, and it fails if x and y are the same location

Michael Mrozek
why would it fail if x == y?
Brian R. Bondy
The answer looks good, but it will not fail if x == y. x becomes zero in the first assignment which can be disconcerting, but this is correct.
Mike Pelley
Michael Mrozek
Would you mind explaining what the operator '^=' is? I've never worked with this and im curious. Even just letting me know the formal name so i can research it would be appreciated.
Grue
@Grue: the name is xor and the operator is ^. ^= means xor and assign with own value.
Brian R. Bondy
The hidden trap with the xor method is when both values occupy the same space in memory. For example, if you have two array indexes into the same array with the same value: `a[i] ^= a[j]` where i and j are the same.
drawnonward
@drawnonward: In his example there is a distinct x and a distinct y.
Brian R. Bondy
@Brain Thanks! I'll have to read about that and see where I can apply it in my code
Grue
+3  A: 

If you have 2 variables a and b: (each variable occupies its own memory address)

a = a xor b
b = a xor b
a = a xor b


There are also some other variations to this problem but they will fail if there is overflow:

a=a+b
b=a-b
a=a-b

a=a*b
b=a/b
a=a/b

The plus and minus variation may work if you have custom types that have + and - operators that make sense.


Note: To avoid confusion, if you have only 1 variable, and 2 references or pointers to it, then all of the above will fail. A check should be made to avoid this.

Unlike a lot of people are saying it does not matter if you have 2 different numbers. It only matters that you have 2 distinct variables where the number exists in 2 different memory addresses.

I.e. this is perfectly valid:

int a = 3;
int b = 3;

a = a ^ b;
b = a ^ b;
a = a ^ b;

assert(a == b);
assert(a == 3);
Brian R. Bondy
I don't believe your xor response is different than the other answers, other than the pseudo-code used to describe xor. xor is commutative, such that "a xor b" is equivalent to "b xor a". The other variations are interesting though - never thought of those ;o)
Mike Pelley
-1 How does you XORing logic handle the case of swapping two references to a same variable or even simple swapping a variable with itself ?
gameover
@Mike: The part that used to be in my answer: "Unlike the other answers, mine works when a == b :)" That was a joke because the other answers originally said that it is important that both values are different. I've edited my answer to remove that line though as to not cause confusion since the other answers were fixed after criticism to them.
Brian R. Bondy
@gameover: who said anything about references or pointers. I said right in my answer "If you have numbers". I don't know why you are assuming that there are any references or pointers involved. There are 2 distinct variables a and b in my explanation.
Brian R. Bondy
@Brain: Please write: If you have two **different** numbers in your answer. That will make your answer correct although incomplete.
gameover
Brian R. Bondy
+4  A: 

You can do it using XOR operator as:

if( x != y) { // this check is very important.

  x ^= y;
  y ^= x;
  x ^= y;
}

EDIT:

Without the additional check the above logic fails to swap the number with itself. Example:

int x = 10;

if I apply the above logic to swap x with itself, without the check I end up having x=0, which is incorrect.

Similarly if I put the logic without the check in a function and call the function to swap two references to the same variable, it fails.

codaddict
@unicornaddict: And why is that check important? -1.
Moron
@moron: I've updated my answer with why the check is very imp.
codaddict
@unicornaddict: Ok, removed the -1, though I would not say that it is very important.
Moron
@Brain: what if the variables were passed by reference ?
codaddict
+1 for pointing out a very crucial fact often 'left out'
pst
+1  A: 

Since no langauge was mentioned, in Python:

y, x = x, y

chpwn