Spiga

Swapping the Variables Without Using Third Variables

Swapping the Variables Without Using Third Variables Using Java

In this post, we will see, how to swap the variables. There are different ways to swap the variables. The major swap algorithms are:
1) Swapping using the third varaibles.
2) Swapping using the Bitwise Operator. This is only for the Integer. This doesn't use the third variables.
3) Swapping using the Arithmetic Operations. This is also, only for the Integer. This also, doesn't use the
    third variables.
4) Swapping the Strings without using the third variables. This is little bit tricky. The logic is similar to the
    above one, i.e. Arithmetic Operations. But the Strings are immutable in the Java, so, externally we are not
    using the third variables, but internally, this uses the third variables.

Below, we have the Java Program to discuss all the above methods in details.

/**
* This class will reverse the two variables.
* Here we will see how to swap the variables using or without using the third variables.
* @author Sonu Mishra
*/
public class SwapVariables {
/**
* Swapping using the third variable and without the third variables.
* Swap two variables using the temporary variables or the third variable.
* @param a The first variable.
* @param b The Second variable.
*/

public static void swapAnyVariablesUsingTempMethod(String a, String b)
{
 System.out.println("Before :: a = "+a+" , b = "+b);
String temp = a;
a=b;
b=temp;
System.out.println("After :: a = "+a+" , b = "+b);
}

/**
* Swapping without using the third variable.
* Swap two variables using the Bit Operator. This Method doesn't use the third variable.
* But this method can't be applied to the String, as the String doesn't support the
* bit operator.
* @param a The first variable.
* @param b The Second variable.
*/

public static void swapIntegerVariablesUsingBitOperator(int a, int b)
{
System.out.println("Before :: a = "+a+" , b = "+b);
a^=b;
b^=a;
a^=b;
System.out.println("After :: a = "+a+" , b = "+b);
}

/**
* Swapping the two integers using the Arithmetic operations,
* and without using the third variables.
* But this method can't be applied to the String, as the String doesn't support the
* arithmetic operations.
* @param a The first variable.
* @param b The Second variable.
*/

public static void swapIntegerVariablesUsingArithmeticOperations(int a, int b)
{
a = a + b;
b = a - b;
a = a - b;
}

/**
* Swapping the two String without using any external third variables.
* Indirectly, this uses the third variable internally in java, as the String is immutable.
* Also, the logic is same as that of the arithmetic operations.
* @param string1 The first String variable.
* @param string2 The Second String variable.
*/

public static void swapStringWithoutUsingThirdVariables(String string1, String string2)
{
System.out.println("Before :: a = "+string1+" , b = "+string2);
string1 = (string1.trim())+(string2.trim()) ;
string2 = string1.substring(0,string1.length()-string2.length());
string1 = string1.substring(string2.length());
System.out.println("After :: a = "+string1+" , b = "+string2);
}

// Main Class to test the above function.
public static void main(String[] args) {
swapAnyVariablesUsingTempMethod( "Sonu", "Mishra");
swapIntegerVariablesUsingBitOperator(5, 7);
swapIntegerVariablesUsingArithmeticOperations(10,12);
swapStringWithoutUsingThirdVariables( "Sonu", "Mishra");
}
}

0 comments: