(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. ;)
— 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!
Do you mean that it can't swap two variables with the same value?
Why is that a problem?
Suppose A and B start with x in them.
Accidentally replied to HosseinYousefi. Reply is for Na2a.
actually this can cause Overflow
This will still work if unsigned types are used.
B=A-B, not B=A=B
A^=B^=A^=B
Undefined behavior
Is it? I thought
^=
operator always executes from right to left. Can you explain, why is it undefined behavior?Because an object must be modified no more than once between two sequence points, but here
A
is modified twice.See this:
https://stackoverflow.com/questions/9958514/
https://stackoverflow.com/questions/5133428/
https://stackoverflow.com/questions/20842602/
Can you explain why, please? I have used it in a couple of competitions(and I've also seen a lot of coders who use this for swapping integers) and the solutions work correctly(except of A=B, as HosseinYousefi pointed out).
See above.
It's equivalent to swapping with one extra variable :)
Although you also get move semantics in C++11!
The best swap is
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.how to swap in java?