13_8_4_3_2_1's blog

By 13_8_4_3_2_1, 9 years ago, In English

(I'm too much infant in case of coding. I'm just writing a blog just out of curiosity)

We know the process of swapping two variables.

Let's 1st talk about a very easy process.

(Swapping with the help of two new variables)

Suppose we have two variables(say A & B). A & B have their values. Now we will take one new variables.(With no values)(Say temp) Then we can do the following: C=A; B=D; C & D are two new variables; Now do the following and print the value of A & B: A=D; B=C; You will see A=3; B=2; Bang!! We have successfully swapped. Let me explain. Let, A & B are two buckets(You can compare variables with buckets, right?) Say, in A we have milk and in B there is honey. We just want honey in A bucket, and milk in bucket B. That's the theory of swapping,right? Okk, now we have brought two new empty buckets C &D(Actually now empty,it has garbage. Let's wash :p) Then what have we done?? We have just put A bucket's milk in C bucket & B bucket's milk in D bucket. Now A& B buckets are empty. Now let's put D bucket's honey in A bucket & C bucket's milk in B bucket Now we have milk in B bucket and honey in A bucket.

(Swapping with the help of only one new variable)

Suppose we have two variables.(say A & B). A & B have their values. Now we will take one new variable.(With no values)(Say temp) Then we can do the following: temp=A; A=B; B=temp;

1st we have assigned the values of A in temp variable, then we have put value of B in A variable. And after that we have assigned value of temp(actually A) in B.

Then after printing the values of A & C, we will see: A=3; B=2; Bang!!! We've successfully swapped two variables.

Let me explain. Let, A & B are two buckets(You can compare variables with buckets, right?) Say, in A we have milk and in B there is honey. We just want honey in A bucket, and milk in bucket B. That's the theory of swapping,right? Okk, now we have brought one empty bucket temp.(Actually now empty,it has garbage. Let's wash :p) Then what have we done?? We have just put A bucket's milk in temp bucket. Now A bucket is empty. Let's put B bucket's honey in A bucket. Now B bucket is empty. Let's just put temp bucket's milk in B bucket. Now we have milk in B bucket and honey in A bucket. Agaain we have successfully swapped. =D

Okk now let's try now try this one.

(Swapping with the help of no extra variables) Suppose we have two variables(say A & B). A & B have their values.

Now let's try this: A=A+B; B=A-B; A=A-B;

I think you have understood. If not , just make comment. ;)

  • Vote: I like it
  • +10
  • Vote: I do not like it

| Write comment?
»
9 years ago, # |
Rev. 2   Vote: I like it +8 Vote: I do not like it

— actually this can cause Overflow.

You can also use xor to swap two variables:

A ^= B; B ^= A; A ^= B;

This also has a problem — It can't swap A and A:

A ^= A; // Now A is zero!

  • »
    »
    9 years ago, # ^ |
    Rev. 2   Vote: I like it +3 Vote: I do not like it

    Do you mean that it can't swap two variables with the same value?

  • »
    »
    9 years ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    Why is that a problem?

    Suppose A and B start with x in them.

    A^=B;   // A=0,B=x
    B^=A;   // A=0,B=x
    A^=B;   // A=x,B=x
    

    Accidentally replied to HosseinYousefi. Reply is for Na2a.

  • »
    »
    9 years ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    actually this can cause Overflow

    This will still work if unsigned types are used.

»
9 years ago, # |
  Vote: I like it +3 Vote: I do not like it

B=A-B, not B=A=B

»
9 years ago, # |
  Vote: I like it +8 Vote: I do not like it

A^=B^=A^=B

»
9 years ago, # |
  Vote: I like it +37 Vote: I do not like it
swap(A, B);
  • »
    »
    9 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    It's equivalent to swapping with one extra variable :)

    • »
      »
      »
      9 years ago, # ^ |
        Vote: I like it +13 Vote: I do not like it

      Although you also get move semantics in C++11!

»
9 years ago, # |
  Vote: I like it +36 Vote: I do not like it

The best swap is

std::swap(a, b);

It tells the compiler the logical intent of the operation, and so the compiler can do its best in optimizing it. Often this will be compiled to a single instruction like XCHG A, B on x86. Compare this to three instructions in case of manually "optimized" XOR.