Arvind Sasikumar
Arvind Sasikumar

Reputation: 502

Is this way of swapping 2 numbers better or worse than the one that uses a temp variable?

Suppose a and b are two integers.

    b+=a;
    a=b-a;
    b-=a;

Is this form of swapping better or worse than using a temp variable? Why? Is one better than the other in some particular cases/scenarios? Assume I am talking here only wrt integer value swapping.

Upvotes: 2

Views: 158

Answers (2)

Chris Beck
Chris Beck

Reputation: 16204

You should say what language you are using first of all.

In general I think its safe to say that "in-place swap" is preferable, from a performance standpoint, to swapping using a temporary.

In C or C++ the above is not good because if you cause integer overflow you get undefined behavior.

In those languages it's better to use bitwise operations for this. The basic observation is that if A and B are bits and we use addition modulo 2, then this is a swap (pseudocode):

a += b;
b += a;
a += b;
// Now A and B are swapped if they are numbers mod 2.

In C / C++ you would therefore use bitwise XOR in the above pattern, to do it to all of the corresponding bits at once.

I believe that's how e.g. the standard swap implementation for two pointers is implemented. (OTOH it's possible that this could just happen as a compiler optimization)

It has the benefit that the in-place version can't fail, while the version that uses a temporary variable can potentially blow the stack if the objects being swapped are too large.

The XOR swap is also substantially better than the naive swap implementation if a and b are smart pointer types -- creating a temporary smart pointer could cause allocations, call a user defined constructor, have side effects, and potentially throw exceptions, meaning that the swap is no longer a no-fail, noexcept function. The XOR swap avoids all of that.

Upvotes: 2

Srinivasulu Rao
Srinivasulu Rao

Reputation: 309

Off course the one which you have used with only two variable are better than using a third variable in it, The reason is for temp variable you have to initialize a value which is off course space consuming and makes the programs nondeterministic and sometime makes it slower by 25%, Secondly good programmers use less number of variable to make the code neat and clean because they know the flow and possibility of the result which the variable may get even if there might be changes in the programming environment, if you are not sure abt consequences of the result because of the ever changing compiling environment then in that case u can use a third variable to at least to get the things on track.

Usually big applications take a lot of variable initialization where there would be thousands or lakhs lines of code hence its better to initialize in a limited manner to get a good performance on the application with less lines of code, hence overwriting a variable is better than creating a new variable, thats a good practice.

Upvotes: -1

Related Questions