PDA

View Full Version : How to swap two variables values without using a third variable?



ReneMiner
06-03-2013, 20:03
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()



How about Strings?



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
'...

ErosOlmi
07-03-2013, 23:46
Thanks a lot Rene. I appreciate!

And what about:
Printl instead of Console_WriteLine
Print instead of Console_Write
Waitkey instead of Console_WaitKey

cbz6109
21-07-2020, 16:44
We can also swap two Numbers using Bitwise operator :bom:


var arg1 = int.Parse(Console.ReadLine());
var arg2 = int.Parse(Console.ReadLine());
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/swap-two-numbers-using-bitwise-operator/

Petr Schreiber
26-07-2020, 17:21
Thank you for sharing the trick,

it looks like this with thinBasic syntax:


uses "console"

print "Please enter the arg1: "
int32 arg1 = console_readline()

print "Please enter the arg2: "
int32 arg2 = console_readline()

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

justin045
29-07-2020, 00:34
Hello,

Little question

Which method is less or more time consuming.:zzz:

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


Regards

ReneMiner
30-07-2020, 10:53
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