sebi's blog

By sebi, 14 years ago, In English

Suppose you have two variables x and y and you want to swap their values. The usual way to do this is using another temporary variable:

     temp = x;
     x = y;
     y = temp;

However, the interchange of two variables can be done without the temp variable:

     x = x + y;
     y = x - y;
     x = x - y;


What is your favourite algorithm for swapping variables?

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it