Four Different Java Programs to Swap Two Numbers Without a Third Variable

Swapping two numbers are done using a third variable. But it is often asked for programming interviews to swap two numbers without using a third variable. We will see four different programs in java to swap two variables without using a third variable. This type of questions are often asked in technical interviews.

Trick 1

In the first java program to swap two numbers without a third variable, we swap the values of the two variables in 3 steps which are simple addition and subtractions.

import java.lang.*;
 
class Swapping
{
public static void main (String[] args) throws java.lang.Exception
    {
    int a=5,b=10;
    a=b+a; //now a =15
    b=a-b; //now b= 5
    a=a-b; // now a=10 (done)
    System.out.println("a = "+a+"  b= "+b);
    }
}


Trick 2

In the second java program to swap two numbers without a third variable, we swap them in a single statement. The statement contains an assignment, addition and subtraction.

import java.lang.*;

class Swapping
{

public static void main (String[] args) throws java.lang.Exception
    {
    int a=10,b=30;
    a=a+b-(b=a);
    //10+30-(10)  a becomes 30
    // b is assigned with 10 in the same line of code
    System.out.println("a= "+a+"  b= "+b);
    }
}

Trick 3

In the third java code, we swap the numbers using bitwise XOR operation. The bitwise operator 
is used in the program. The binary representation of the numbers, the bitwise xor operation on them and the result are shown in the c program as comments. For simplicity and convenience, we are representing numbers using only 5 bits. The real size of an integer in java is much larger. We show only the rightmost 5 bits. It is enough to illustrate the examples with small integers. The program is as follows:


import java.lang.*;

class Swapping
{

public static void main (String[] args) throws java.lang.Exception
    {
    int a=8,b=12;
    
   /* a: 8 : 01000
      b:12 : 01100
    */
   
    a=a^b; 
   
    /*  a:  01000 ^
        b:  01100
        a=  00100 (4)
    */

    b=a^b;

    /*a:    00100 ^
      b:    01100
      b=    01000 (8)
    */

    a=b^a;

    /* b:   01000 ^
       a:   00100 
       a=   01100 (12)
    */

    System.out.println("a= "+a+"  b= "+b);
    }
}

Trick 4

In the fourth java program to swap two numbers without a temporary variable, we swap the numbers using a combination of addition, subtraction and bitwise NOT (inversion) operation. The bitwise operator is used in the program. The binary representation of the numbers, the bitwise NOT operation on them and the result are shown in the java program as comments. The program is as follows:


import java.lang.*;

class Swapping
{

public static void main (String[] args) throws java.lang.Exception
     {
     int a=14,b=4;
    
   /* a: 14 : 1110
      b:  4 : 0100
     ~a:   :  0001  
     ~b:   :  1011  
   */
   a=b-~a-1;
    /* b:   0100 -
      ~a:   0001
            0011 -
       1:   0001
            0010
     now a= 0010 
    */
    b=a+~b+1;
    /* a:   0010 +
      ~b:   1011
       1:   0001
    now b:  1110  (14)
       ~b:  0001
    */
    a=a+~b+1;
   /*  a:   0010 +
      ~b:   0001
       1:   0001
    now a:  0100 (4)
   */

   System.out.println("a= "+a+"  b= "+b);
   }
}

No comments:

Post a Comment