the cde tlks Understanding the language of code as it speaks
Swapping the Swap - From Numbers To String
Swapping the Swap - From Numbers To String

Ever encountered some program or code where you would have needed to swap the two numbers or data. And then you would think.. okay well how would I do it? Here we are to find different ways to swap two different datasets. Let's dive into them.

Below are couple of ways where we can swap two different numbers.


USING AN EXTRA VARIABLE

This way is the most straightforward and the most easier to understand. It's just about putting value in one external bucket and then exchanging the values. Below is the step of doing this.

For var a = 10, b = 15

we can have additional variable say var temp; And we can start the swap as below.

var a = 10, b = 15

var temp;

temp = a;

a = b;

b = temp;

This approach as mentioned above is the most easiest and it is not just with integers but also with string data we can use this approach.


USING ADDITION / SUBTRACTION

This approach only takes two variables instead of three. This involves certain arithmetic operations which we can leverage.

var a = 10, b = 15

a = a+b;

b = a-b;

a = a-b;

Let's understand this further. 

a + b would give us 25, so a would be 25

now when we say b = a - b we would be doing 25-15 which would be 10, so the value of a is now transferred to b. So b = 10

Then when we say a = a-b again it would be a = 25-10 it would be 15 so the resulting value for a would be 15.

So the final value would be a = 15, b = 10.

Caveat: Although this method would work in normal cases, there are certain cases when there could be 

  1. Overflow issues for example we have two integers and if the addition of two integers crosses the max limit of integer. 
  2. Addition subtraction might lead to loss of precision in case of numbers being floating point like 1.234 or 3.47261.
  3. Also not applicable in case the data is string 


USING MULTIPLICATION / DIVISION

This approach only takes two variables instead of three. This involves certain arithmetic operations which we can leverage.

var a = 10, b = 15

a = a*b;

b = a/b;

a = a/b;

Let's understand this further. 

a*b would give us 150, so a would be 150

now when we say b = a / b we would be doing 150 / 15 which would be 10, so the value of a is now transferred to b. So b = 10

Then when we say a = a / b again it would be a = 150 / 10 it would be 15 so the resulting value for a would be 15.

So the final value would be a = 15, b = 10.

Caveat: This method has a small issue whereas if we have one of the number as 0 then it would cause an issue. For example 

If we have a = 15, b  = 0; a  would be 0 on first multiplication and b would be 0 as well because 0/0 would be 0 itself. Same would be the case for when we have a = 0, b  = 15.

So this method would only work if the numbers are non zero. Also overflow issues and precision loss as mentioned in the previous method can happen. Also not applicable in case the data is string


USING XOR OPERATION

XOR operations work on the binary representation of numbers. It is 1 when the two inputs are different i.e. 1 and 0. It would return 0 when two inputs are same.

aba XOR(^) b
000
011
101
110

For example the number 8 would be represented in binary as 1000, the number 3 would be represented in binary as 0011. Now 8 XOR 3 would be 1000 XOR 0011 which would make 1011 making the result 11.

This works best of integer operations.

var a = 10, b = 15

a = a^b;

b = a^b;

a = a^b;

Let's understand this further. 

a^b would be basically 10 (1010) XOR(^) 15(1111) which would result in 0101 i.e a = 5

now when we say b = a ^ b we would be doing 5(0101) ^ 15(1111) which would be 1010(10), so the value of a is now transferred to b. So b = 10

Then when we say a = a ^ b again it would be a = 5(0101) / 10(1010) it would be (1111)15 so the resulting value for a would be 15.

So the final value would be a = 15, b = 10.

Caveat: This method performs best when we talk in terms of integers. However when it comes to floats i.e. numbers with precision it could cause some issues.


SUBSTRING APPROACH

This approach is more useful when it comes to swapping two datasets which are string.

var a = 'Hello', b= 'World'

// 'Hello' + 'World' concatenation of string would be make a = 'HelloWorld'

a = a+b; 

// b would be assigned the text starting from 0 to the result of substraction

// a.length = 10, b.length = 5 , which means 10-5=5 so be would be assinged substring of a from length 0 to 5 which is Hello

b = a.substring(0,a.length-b.length) 

// a would be assigned substring starting from b.length which is 5 so a would be assigned a value World

a = a.substring(b.length, a.length)

Hence the swap!

Well we have learned a lot about how we can swap the data using an extra variable or without using an extra variable. The extra temp variable is kind of the most easiest to implement and code which also brings out a better clarity.  Also it avoid the caveats present in the other methods as well. Having knowledge though about different methods certainly helps us in broadening our thought process.

That's it for now, until next time where we could dive into some other talks which we will have with the code!!

Powered by Froala Editor