Tyler Poff


Swapping 2 Variables Without a Temp Variable

05-22-2019

So in programming you have variables, lots of variables, and sometimes you need to swap the value of 2 variables for whatever reason. So the value of X needs to become the value of Y and Y needs to be X. This is a basic problem to solve and was one of the first things that I learned when I started programming. Create a temporary variable to hold one of the values and then swap them around. For years I thought that this was the only real way you could swap them. But as it turns out, it's not.

So, I'm either about to blow your mind, or convince you I'm an idiot. Are you ready? When you're programming, you can swap 2 variables WITHOUT using a temporary variable to hold one.

Yeah. Mind blown right? No? oh...


The Traditional Way

This was originally brought to my attention by a TA in one of my intro to programming classes in college, and when he explained how it was possible it blew my mind. Before I had always though you had to use a temporary variable, like the example below.

x = 1
y = 2
temp_variable = x
x = y
y = temp_variable

So when we began the sample above, x was 1, and y was 2. When we ended y was 1, and x was 2. We swapped their values by temporarily holding the value of x in another variable. And this works. This is how I was first taught how to swap variables, it's reliable, and it makes sense. It also works for any data type.

But, as my bored looking TA explained in class one day "it's not wrong, but you can do it without the temporary variable." And, with a huge sigh the TA reluctantly went to the whiteboard to show us programming novices a thing or two.


The Cool Kid Way

Without further ado, here's how to do it without a temp variable.

x = 1
y = 
x = x + y
y = x - y
x = x - y

And, believe it or not, at the end of this script, x = 2, and y = 1. It may not be obvious like this, so let's break it down a bit more. This is how my TA explained it way back when after we confessed our confussion on how the above was working.

x = 1
y = 2
x = x + y
// At this point, x = 1 + 2
// Don't think about it as x = 3, think about it in terms of the seperate numbers,
//so, x = 1 + 2 and y = 2

y = x - y
// So now, y = x - y = (1 + 2) - 2
// simplifying it now gives us y = 1, which is the value of x.
// at this point x = 1 + 2 still, but now y has the old value of x which was 1.

x = x - y
// or, x = (1 + 2) - 1 = 2
// which means we now have x = 2, y = 1
//and we're done!

So yeah, the math checks out. Like I said, this is blew my mind when I first heard about it, but it's pretty straight forward. And I'm sure most of you are underwhelmed.

One thing to note is this assumes that you can freely add and subtract whatever object you are trying to swap and their values remain relative to each other. So if you have some more abstract objects that can't be added for example, then this method doesn't work. But if you're trying to swap numbers around, this is a pretty neet way to do it.

In conclusion, simple mind, simple pleasures, and this method of swapping variables pleases me greatly :-)

Page Links