swap the value of two variable Four method [Day-02]

swap the value of two variable Four method [Day-02]

This is day 2 of learn DSA in this particular blog we learn about the how the swap the value of two variable.

swap the value of two variable is important concept because many of sorting algrorithm used this conecpt for eg. Bubble Sort , Insertion Sort and many algorithm.

We discussed three to four method swap the value of two variable.

input : 2 4
Output : 4 2
input : 5 0
Output : 0 5

Note : Second , third and fourth method is asked in many interviews so clearly understand these algorithm.

First Method
Take four variable and store the value of 2 variable into 2 another variabel and print this . This is word method because we take extra space.


import java.util.Scanner; public class Second { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter any two numbers"); int firstNum = sc.nextInt(); int secondNum = sc.nextInt(); int first = secondNum; int second = firstNum ; System.out.println("After swaping " + first + " " + second); sc.close(); }
} 
Enter fullscreen mode Exit fullscreen mode

Second Method
use the third variable and swap the value .
Approach :
take a third variable temp initially temp has null value or say nothing have a value .
in first step give value of first variable to temp.
in second step give value of first varibale to second num .
in third step give value of second variable to temp variable.

Code
import java.util.Scanner; public class Second { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter any two numbers"); int firstNum = sc.nextInt(); int secondNum = sc.nextInt(); int temp ; temp = firstNum ; firstNum = secondNum ; secondNum = temp ; System.out.println("After swaping " + firstNum + " " + secondNum); sc.close(); }
} 
Enter fullscreen mode Exit fullscreen mode

Third Method:
without using any third variable to swap the value of two variable.
Approach :
firstNum = fristNum + secondNum;
secondNum = fristNum – secondNum ;
firstNum = fristNum – secondNum;

import java.util.Scanner; public class Second { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter any two numbers"); int firstNum = sc.nextInt(); int secondNum = sc.nextInt(); firstNum = firstNum + secondNum ; secondNum = firstNum - secondNum ; firstNum = firstNum - secondNum ; System.out.println("After swaping " + firstNum + " " + secondNum); sc.close(); }
} 
Enter fullscreen mode Exit fullscreen mode

*Fourth Method *
This method is awsome it take less time to another method which we discussed above. we use the concept of bit manipulation.
we take the xor(^) of two numbers.
But first understand xor (^) operator


0 ^ 0 = 0
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1

xor(^) of two different value is always 1 but xor(^) of two same thing is 0

Code

import java.util.Scanner; public class Second { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter any two numbers"); int firstNum = sc.nextInt(); int secondNum = sc.nextInt(); firstNum = firstNum ^ secondNum ; secondNum = firstNum ^ secondNum ; firstNum = firstNum ^ secondNum ; System.out.println("After swaping " + firstNum + " " + secondNum); sc.close(); }
}
Enter fullscreen mode Exit fullscreen mode

if you have any doubt please tell us in comment I try to answer your question.

if you not familiar with our dsa series please follow me .


Discover more from Coursity

Subscribe to get the latest posts to your email.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *