Programmers should know how to swap two variables because it allows them to work effectively with any type of data. In this article, we will learn how to do swapping in C++ with or without a function.
What is Swapping?
Swapping is the process of exchanging the values of two variables. Assume you have two cups, one full of water and the other empty. Switching those glasses involves pouring water from one cup into the other, such that the previously empty cup now contains water and the previously full cup is now empty.
We can do the exact same thing with variables in programming.
For instance, suppose you have two variables: A and B. A currently has a value of 5, while B currently has a value of 10. If you want to swap the values of these two variables, you may do it with a swapping function in C++. A will have a value of 10 after the exchange, while B will have a value of 5.
Swapping between two variables can be achieved in 3 ways in C++. The first method is to use a built-in swap function, or by creating a function, or without a function.
C++ Swap Function
The simplest method of swapping two variables is to use the built-in function.
The swap function in C++ is included in the standard library std::swap used to exchange the values of two objects of the same type. It can be used by simply making a function call with the two variables passed as parameters.
The code implementation is given below:
#include using namespace std; int main() { int x = 5; int y = 10; cout << "Before swapping: " << endl; cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "After swapping: " << endl; cout << "x = " << x << ", y = " << y << endl; return 0; }
Output:
Before swapping: x = 5, y = 10 After swapping: x = 10, y = 5
The time complexity of the C++ swap function is O(1). This means that the time taken to swap any two objects is the same, regardless of the size or complexity of the objects being swapped.
How to Swap using a Function in C++?
Let us say we want to implement the operation of swapping variables in the form of a function. This would allow us to implement modularity and reuse code in our program. Swapping two variables inside a function would require us to pass the values of the variables to the function before swapping. Values can be passed to a function either by value or by reference.
Let us have a look at both methods with examples:
#include using namespace std; void swap(int a, int b) { int temp = a; a = b; b = temp; } int main() { int x = 5; int y = 10; cout << "Before swapping: " << endl; cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "After swapping: " << endl; cout << "x = " << x << ", y = " << y << endl; return 0; }
Output:
Before swapping: x = 5, y = 10 After swapping: x = 5, y = 10
This code implements a call-by-value function. What we observe here is that the values of the variables have not changed. This is because the call be value only maintains a copy of the variables and not the actual variable. Therefore if you want to swap two variables you will have to use call by reference function.
The code implementation is given below:
#include using namespace std; void swap(int& a, int& b) { int temp = a; a = b; b = temp; } int main() { int x = 5; int y = 10; cout << "Before swapping: " << endl; cout << "x = " << x << ", y = " << y << endl; swap(x, y); cout << "After swapping: " << endl; cout << "x = " << x << ", y = " << y << endl; return 0; }
Output:
Before swapping: x = 5, y = 10 After swapping: x = 10, y = 5
How to Swap without a Function in C++?
In this section, we will swap two variables without declaring any special function to do so. There are two ways in which we can go about this operation. The first method is to use a third variable to perform the swap. The second method is to do the swapping operation without using a third variable but by using arithmetic operations.
1) Using a Third Variable
The easiest method to swap two values in C++ without a function is by using a third variable.
In this method, we create a temporary variable. This temporary variable stores the value of the first variable. Then we store the value of the second variable in the first variable. Lastly, we replace the value in the second variable with the first variable. That way the swapping becomes complete.
Here is an example of this:
#include using namespace std; int main() { int a = 5, b = 10, temp; cout << "Before swapping: a = " << a << ", b = " << b << endl; // Swapping using a third variable temp = a; a = b; b = temp; cout << "After swapping: a = " << a << ", b = " << b << endl; return 0; }
Output:
Before swapping: a = 5, b = 10 After swapping: a = 10, b = 5
In this code, we need to swap two variables A and B. In order to do that we create a third variable called temp. We store the value of A in temp first. This is to ensure we do not lose this value in the next step. The next step is replacing the value of A with the value of B.
After we have done that, we now need to bring the value of A in B to complete the swapping. Here we make use of the third variable. The temp variable stores the value of A already. We replace the value of B with the value of temp, effectively replacing the value of B with the value of A. Now our swapping is complete and we print the swapped variable.
2) Using Arithmetic Operations
In this method, we use either adding and subtraction or division and multiplication to swap two variables. Here is an example:
#include using namespace std; int main() { int a = 5, b = 10; cout << "Before swapping: a = " << a << ", b = " << b << endl; // Swapping using addition and subtraction a = a + b; b = a - b; a = a - b; cout << "After swapping: a = " << a << ", b = " << b << endl; return 0; }
Output:
Before swapping: a = 5, b = 10 After swapping: a = 10, b = 5
In this code, we have used addition to replace the values of A and B. We first replace the value of A with the sum of the two variables. Then from this sum, we subtract the value of B and put that value in B. This effectively means B now has the value of A.
Now from the sum of the two values, we subtract the value of the variable B. B stores the value of A, which means we are effectively subtracting the value of A from the sum. We put the subtracted value in variable A and now we have completed swapping the two variables.
Let us have a look at another way to implement this method to better understand it:
#include using namespace std; int main() { int a = 5, b = 10; cout << "Before swapping: a = " << a << ", b = " << b << endl; // Swapping using multiplication and division a = a * b; b = a / b; a = a / b; cout << "After swapping: a = " << a << ", b = " << b << endl; return 0; }
Output:
Before swapping: a = 5, b = 10 After swapping: a = 10, b = 5
In this method, we use division and multiplication to swap the values of A and B. We first store the product of A and B in variable B.
Then we divide the product by B and store the value in B. This effectively means putting the value of A in B. Then we use this updated value of variable B and divide the product of A and B with this variable value. This divided value we put in variable A, effectively putting the value of B in A. The swapping is now completed.
Conclusion
In this article, we have learned the various methods that are available when we want to swap two variables in C++ which also includes the built-in swap function. Happy Learning :)