Swapping Two Variable Value Without Using Third Variable

When would you swap two numbers without using a third variable?

At this point it's just a neat trick. Speed-wise if it makes sense though your compiler will recognize a normal swap and optimize it appropriately (but no guarantees that it will recognize weird xoring and optimize that appropriately).

Swap two variables value without using third variable in php

This method will work for any variable type:

$a = 5;
$b = 6;
list($a, $b) = array($b, $a);
print $a . ',' . $b;

Output:

6,5

Another simple way (which only works for numbers, not strings/arrays/etc) is

$a =  $a + $b;  // 5 + 6 = 11
$b = $a - $b; // 11 - 6 = 5
$a = $a - $b; // 11 - 5 = 6
print $a . ',' . $b;

Output:

6,5

Swapping object variables in Javascript without using a 3rd variable

var a=1,
b=2,
output=document.getElementById('output');
output.innerHTML="<p>Original: "+a+", "+b+"</p>";
b = [a, a = b][0];
output.innerHTML+="<p>Swapped: "+a+", "+b+"</p>";

<div id="output"></div>

Swapping two variable value without using third variable

Using the xor swap algorithm

void xorSwap (int* x, int* y) {
if (x != y) { //ensure that memory locations are different
*x ^= *y;
*y ^= *x;
*x ^= *y;
}
}



Why the test?

The test is to ensure that x and y have different memory locations (rather than different values). This is because (p xor p) = 0 and if both x and y share the same memory location, when one is set to 0, both are set to 0.
When both *x and *y are 0, all other xor operations on *x and *y will equal 0 (as they are the same), which means that the function will set both *x and *y set to 0.

If they have the same values but not the same memory location, everything works as expected

*x = 0011
*y = 0011
//Note, x and y do not share an address. x != y

*x = *x xor *y //*x = 0011 xor 0011
//So *x is 0000

*y = *x xor *y //*y = 0000 xor 0011
//So *y is 0011

*x = *x xor *y //*x = 0000 xor 0011
//So *x is 0011



Should this be used?

In general cases, no. The compiler will optimize away the temporary variable and given that swapping is a common procedure it should output the optimum machine code for your platform.

Take for example this quick test program written in C.

#include <stdlib.h>
#include <math.h>

#define USE_XOR

void xorSwap(int* x, int *y){
if ( x != y ){
*x ^= *y;
*y ^= *x;
*x ^= *y;
}
}

void tempSwap(int* x, int* y){
int t;
t = *y;
*y = *x;
*x = t;
}


int main(int argc, char* argv[]){
int x = 4;
int y = 5;
int z = pow(2,28);
while ( z-- ){
# ifdef USE_XOR
xorSwap(&x,&y);
# else
tempSwap(&x, &y);
# endif
}
return x + y;
}

Compiled using:

gcc -Os main.c -o swap

The xor version takes

real    0m2.068s
user 0m2.048s
sys 0m0.000s

Where as the version with the temporary variable takes:

real    0m0.543s
user 0m0.540s
sys 0m0.000s

How do you swap two variables of any type without using third in swift?

Use tuples!

var a = "a"
var b = "b"
(b, a) = (a, b)

This works for any type, or any number of variables.

Another way that is similar to your approach with Ints:

a += b
b = String(a.dropLast(b.count))
a = String(a.dropFirst(b.count))

R: Swap two variables without using a third

For integers, you can use

a = a + b
b = a - b
a = a - b

and for strings, this would work

a <- "one"
b <- "two"
a <- paste(a,b, sep = "")
b <- substr(a,0,nchar(a) - nchar(b))
a <- substr(a,nchar(b) + 1, nchar(a))

> a
# two
> b
# one

Swap two variables without using a temporary variable

First of all, swapping without a temporary variable in a language as C# is a very bad idea.

But for the sake of answer, you can use this code:

startAngle = startAngle + stopAngle;
stopAngle = startAngle - stopAngle;
startAngle = startAngle - stopAngle;

Problems can however occur with rounding off if the two numbers differ largely. This is due to the nature of floating point numbers.

If you want to hide the temporary variable, you can use a utility method:

public static class Foo {

public static void Swap<T> (ref T lhs, ref T rhs) {
T temp = lhs;
lhs = rhs;
rhs = temp;
}
}


Related Topics



Leave a reply



Submit