Different Swap implementation;

0 views
Skip to first unread message

NItin

unread,
Dec 11, 2007, 3:58:51 AM12/11/07
to Pune Java Interview Questions Group, niti...@gmail.com
Hi all,

Different Swap implementation:

.................................................................

Implementation 1: Textbook swap()
.........................................................
The implementation of the plain, vanilla swap() algorithm is
straightforward:
1. Take two arguments of the same type,
2. Copy the first one to a temporary variable,
3. Assign the second to the first, and finally,
4. Assign the temporary value to the second.

Here is the complete function template:

template<class T> void v_swap(T& t1, T& t2)//plain vanilla
{
T temp=t1;
t1=t2;
t2=temp;
}
Advantage:
It's generic. And it's applicable to built-in types and user-defined
types alike and more readable code.


.............................................................................
Implementation 2: Swapping Without a Temporary
................................................................

The trick is to use the += and -= operators, thereby performing two
operations in a single expression.

Here is the complete implementation of this function template:

template <class T> void nt_swap(T& i, T& j) //no temporary
{
i -= j;
j += i; // j gets the original value of i
i = (j - i); // i gets the original value of j
}


Advantage:
This implementation is applicable to built-in types and user-defined
types that overload the += and -= properly

Disadvantage:
More cryptic code

Comparing the above two impln:
There is not statistically-significant differences between the these
two swap() implementations when applied to integers.
When applied to double, the plain vanilla wins big time. Its execution
speed was less than a third of nt_swap()'s execution speed!

...........................................................................................................

Implementation 3: Swapping Without a Temporary (with bitwise operator)
.........................................................................................
uses the bitwise operators to eliminate a temporary variable. The
disadvantage is that this version can only take arguments of integral
types.

void nt2_swap(int& i, int& j)//temporary-free, with bitwise operator
{
i ^= j;
j ^= i;
i ^= j;
}// i and j have been swapped

Advantage:
No such advantage.

Disadvantage:
More cryptic code and only works when arguments for swapping are of
integral types

Comparing the above three impln:
Performance-wise, this implementation doesn't differ significantly
from the previous two.



Choosing the swap implementation:

1. nt_swap() and nt2_swap() (Swapping Without a Temporary approach)
might be useful in certain cases: when maintaining legacy code.

2. However, in production code, generic plain vanilla swap is the
preferable choice.

Reference link : http://www.devx.com/getHelpOn/Article/11395/0/page/1


Thanks
Nitin,

Rohit Ghatol

unread,
Dec 11, 2007, 4:01:36 AM12/11/07
to Pune Java Interview Questions Group
This is a good example.

To phrase it as a puzzle i would say
Give different Implementations of swap functionality in Java
We should expect two implementations from the candidate, and then give
him/her hints to think of the third alternative.

Good work, keep them coming! :)
Reply all
Reply to author
Forward
0 new messages