Wednesday, August 25, 2004

swap

How many variables do you need in order for you to do a swap? Did I hear 3? Provided that we are talking about numerical values, we only need 2.

Swapping using three variables

A = B
B = C
C = A

Swapping using 2 variables

A = A + B
B = A - B
A = A - B