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?

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

| Write comment?
14 years ago, # |
  Vote: I like it +3 Vote: I do not like it
Another alternative (if x and y don't share the same memory, as in the +/- case) is
x ^= y ^= x ^= y;
though this exact wording is not recommended.
  • 14 years ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    it is do not work in java. this code in C++.


    • 14 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it
      Yes, it doesn't work, because the expression is being calculated from left to right, so the first instance of variable x has the same value as it was before that line, despite of that logic says that it should be changed before we can do first XOR.
14 years ago, # |
  Vote: I like it 0 Vote: I do not like it
> What is your favourite algorithm for swapping variables?

     t = x;
     x = y;
     y = t;
14 years ago, # |
  Vote: I like it 0 Vote: I do not like it
if x, y - int, double ... swap(x,y) :)
else
p = x;
x = y;
y = t;
  • 14 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
    p = x;  => t=x;
  • 14 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it
    AFAIR, swap is template function, so I don;t understand why special case for non built-in type
    • 14 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it
      Hmm... it`s true... I think swap can't swapping structs and other variable...
      • 14 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it
        Why don't?? Swap can swapping structs and objects
        • 14 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Oh, sorry... my bad English... it`s in past time...

          I say, that swap can swapping structs and other variable...

      • 14 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it
        It can.
        • 14 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it
          int x = 1;
          int y = 3;
          System.out.println("Before swap x=" + x + " , y=" + y);
          x = x ^ y;
          y = x ^ y;
          x = x ^ y;
          System.out.println("After swap x=" + x + " , y=" + y);

          This is similar to earlier mentioned 
          • 14 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it
            In Java you have to write it yourself (by XOR or with temporary variable), because it's impossible to write swap(x, y) in Java...And I don't like it .. It would be great if there were something like ref/out parameters in C#
14 years ago, # |
  Vote: I like it 0 Vote: I do not like it
Where are you from, lad?
14 years ago, # |
  Vote: I like it 0 Vote: I do not like it
a, b = b, a