Swap Two Variables

This is a common action that comes up in most algorithms, or even just programming in general. Given two variables (or “things”, some sort of data), make the first one the second one and the second one the first one.

Imagine that scene in Indiana Jones.

../../_images/the_Idol.png

Yeah, that one. (If you don’t know what I’m talking about, this is the full scene.)

So, to do a similar thing, we do the following:

  • Make a temporary variable

  • Assign the first variable to the temporary variable

  • Assign the second variable to the first variable

  • Assign the temporary variable to the second variable

“There’s a third variable, what gives?”

If you think about it, Indiana jones has two hands, and there was one spot for the artifact. All three were used. For the same reason, we have three variables, three places for storing the data. That’s just how this is done. The examples:

#include <iostream>
using namespace std;

int main(){
  string first = "first";
  string second = "second";
  cout << first << " " << second << endl;

  string temp = first;
  first = second;
  second = temp;
  cout << first << " " << second << endl;

  //native solution
  swap(first, second);
  cout << first << " " << second << endl;

  return 0;
}
class Program {
  static void Main(string[] args)
  {
    string first = "first";
    string second = "second";
    System.Console.WriteLine(first + " " + second);

    string temp = first;
    first = second;
    second = temp;
    System.Console.WriteLine(first + " " + second);
  }
}
public class swap {
  public static void main(String[] args) {
    String first = "first";
    String second = "second";
    System.out.println(first + " " + second);

    String temp = first;
    first = second;
    second = temp;
    System.out.println(first + " " + second);
  }
}
var first = "first";
var second = "second";
console.log(first, " ", second);

var temp = first;
first = second;
second = temp;
console.log(first, " ", second);
first = "first"
second = "second"
print(first, second)

temp = first
first = second
second = temp

print(first, second)

#'native' solution (this also works)
first, second = second, first
print(first, second)
var first = "first"
var second = "second"
print(first, second)

let temp = first
first = second
second = temp
print(first, second)

Notes

The standard library has a swap function that works with any type, with the syntax swap(firstVariable, secondVariable);.

Note

For the super involved: std::swap() in anything earlier than C++ 11 uses the standard three-variable swap, the same thing presented here. C++ 11 and later uses std::move() internally when using std::swap(), meaning less copies of data are made when possible. It’s more efficient, but generally it doesn’t matter.

There is no native C# method, this is the standard way to do it.

There is no native swap method in Java, this is standard.

Standard fare.

The syntax first, second = second, first makes a tuple (like an array but can have different types) temporarily, and this is optimized by the compiler. This may be more common in python than the standard three-variable method, but either are acceptable.

# is used to make comments in python (shift 3).

We can make the temporary variable a “constant” (unchanging) variable by using let instead of var. This is more efficient, and tells the compiler that we won’t be changing the value of the temporary variable. Use this when you can.