In this blog, we will look into several ways of implementing swap in different ways.
Intermedia Variable
int a = 1;
int b = 2;
int tem = a;
a = b;
b = temp;
Addition and Subtraction
int a = 1;
int b = 2;
a = a+b;
b = a-b;
a = a-b;
Multiplication and Division
int a = 1;
int b = 2;
a = a*b;
b = a/b;
a = a/b;
XOR
int a = 1;
int b = 2;
a = a^b;
b = b^a; // b = b^(a^b) = b^a^b = b^b^a = 0^a = a
a = a^b; // a = (a^b)^a = a^b^a = a^a^b = 0^b = b