# Thread: How to swap two variables values without using a third variable?

1. ## Do you know: How to swap two variables values without using a third variable?

just do some math:

```Uses "Console"

Double a = 1.234
Double b = 5.678

console_writeLine("A = " + str\$(a) )
console_writeLine("B = " + str\$(b) )

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

console_writeLine("A = " + str\$(a) )
console_writeLine("B = " + str\$(b) )

console_waitKey()
```

```Uses "Console"

String A = "World !"
String B = "Hello "

Console_WriteLine(A + B)

A += B
B = Left\$( A, Len(A) - Len(B) )
A = Right\$(A, Len(A) - Len(B) )

console_writeLine(A + B)

console_waitKey()
```
or in thinBasic:
```'...
Swap A,B
'...
```

2. Thanks a lot Rene. I appreciate!

3. ## Swap two Number using Bitwise operator

We can also swap two Numbers using Bitwise operator

Console.WriteLine("\n Before swapping arg1= {0} and arg2 = {1}", arg1, arg2);
arg1 = arg1 ^ arg2;
arg2 = arg1 ^ arg2;
arg1 = arg1 ^ arg2;

Here is full sample code
https://corevoila.in/coding-problems...wise-operator/

4. Thank you for sharing the trick,

it looks like this with thinBasic syntax:
```uses "console"

print "Please enter the arg1: "

print "Please enter the arg2: "

Printl(strformat\$("Before swapping arg1= {1} and arg2= {2}", arg1, arg2))

arg1 = arg1 xor arg2
arg2 = arg1 xor arg2
arg1 = arg1 xor arg2

Printl(strformat\$("After swapping arg1= {1} and arg2= {2}", arg1, arg2))

WaitKey
```

Petr

5. ## And what about compute time

Hello,

Little question

Which method is less or more time consuming.

The time may be an important parameter, example bubble sorting.

Regards

6. Therefore i would suggest If your Bubbles are Stores to a String-Array to use Array Sort.
If you stored values in a different manner - f.e. stored at heap-memory or AS subelements of udt it's probably still faster to sort the even numeric Elements as strings using the workaround and take advantage of a special behaviour of the array-functions - Here Array Extract - which will Sort the source-Array before extracting any values since the thinBasic-array-handling functions are incredible fast.
So depending in how you stored Bubbles i would either convert them into an array or make the pointers to the bubbles an array to make use of array-functions -
When your program uses "Dictionary" to store the data that is- in any case- the fastest and Most straightforward solution: it offers to sort however you like and it does not require to transfer any data before sorting. If you need to sort often in your program its worth to consider the use of Dictionary-module.
If you are using "UI"-module anyway, create a listbox (if your Bubbles are values in an array of simple datataype, Listview If you have Data in an udt-array). Set its sorted-property true and Set to ascend or descend, fill in numeric values ("nVal") with leading zeros- or If in range of 32bit integer (Long, DWord) simply using Hex\$(nVal, 8 ) :
8 makes it formatted to 8 digits and all displayable values while f.e. MKE\$(nVal), -range of Extended number-will result in a String containing Digits that can not be displayed if not re-converted (use CV...-functions). The list can stay invisible (mainly its enabled).
It might not be the fastest way to sort - but very simple and it's easy to keep overview - you know anytime where your values are and what they are doing.
And for the thinbasic-core-array-functions as said - If it's obviously not offered by Array Sort - use Array Extract with an easy extraction-pattern or even to select a few Special Elements and "abuse" the behaviour of Array Extract that will Sort the source-Array.
Array Extract even offers to Extract pointed Elements - so you could make use of its behaviour to sort the source-Array and collect the VarPtr()s to your bubbles in an ordinary Array of DWord-values

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•