x=9;
y=4;
x=x+y; #x=13;y=4;
y=x-y; #y=13-4=9;
x=x-y; #x=13-9=4;
flow chart to swap two number
a=a+b; b=a-b; a=a-b;
There are three primary algorithms to exchange the values of two variables. Exchange with Temporary Variable temp = a; a = b; b = temp; Exchange Without Temporary Variable Using Exclusive Or a = a ^ b; b = b ^ a; a = a ^ b; Exchange Without Temporary Variable Using Arithmetic a = a + b; b = b - a; a = a - b;
To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;
To swap the values of two variables, x and y, using a temporary variable t: t = x; x = y; y = t; To implement this algorithm as a function, the variables must be passed by reference. For efficiency, particularly with large or complex objects that are expensive to copy, use move semantics. The C++ standard library provides the following implementation (since C++11): namespace std { template<typename T> void swap (T&& x, T&& y) { T t = std::move (x); x = std::move (y); y = std::move (t); } }; The C++ standard library swap function (std::swap) uses the above implementation (since C++11). If a type does not support the move semantic, copy semantics will be used instead.
Consider the following declarations:int x = 0;int y = 1;In order to swap the values, we need to use a temporary variable:int t = x;x = y;y = t;However, it is possible to swap the values without using a third variable:x ^= y ^= x ^= y;
It means that you swap the values of that variables EX: -==- before swapping :- Variable1 = 5; variable2 = 10; after swapping :- Variable1 = 10; variable2 = 5;
Use list assignment i.e. for two variables $a, $b: ($a,$b) = ($b,$a)
#include<iostream> using namespace std; void swap(int &i, int &j) { int temp = i; i = j; j = temp; } int main() { int i,j; cin>>i>>j; cout << i << j << endl; swap(i,j); cout << i << j; return 0; }
By using the algorithm of bitwise EORing (Exclusive ORing) the numbers together:If the two numbers are X and Y, then to swap them:X = X EOR YY = Y EOR XX =X EOR Ywill swap them.With knowledge of that algorithm, one then uses the syntax of Javascript to implement it.
You'll need 3 variables for this, here's a pseudo code for swapping values of 2 variables.ALGORITHM SWAPvar1 = 10var2 = 20temp = 0temp = var1 "temp = 10"var1 = var2 "var1 = 20, originally 10"var2 = temp "var2 = 10, originally 20"END SWAP
To swap the highest and lowest values in an array, follow these steps: Initialize two variables, maxIndex and minIndex, to track the indices of the maximum and minimum values, respectively. Iterate through the array to find maxIndex (the index of the highest value) and minIndex (the index of the lowest value). Once both indices are found, swap the values at these indices. Here’s a simple pseudocode representation: function swapHighestLowest(arr): maxIndex = 0 minIndex = 0 for i from 1 to length(arr) - 1: if arr[i] > arr[maxIndex]: maxIndex = i if arr[i] < arr[minIndex]: minIndex = i swap(arr[maxIndex], arr[minIndex])