In order to swap two values in computer memory you need to temporarily store one of those values during the exchange. That is, to swap the values of object's A and B we must perform the following steps where T is a temporary object:
Note that this algorithm does not implement a "perfect swap". That is, when two people (the objects) swap their house keys (the values), we don't require a third person (the temporary object) to hold a copy of one of those keys, we simply hand the actual keys over (perhaps even simultaneously). No copies are made or indeed required -- we simply "move" values from one place to another.
But a computer cannot move values from one place to another, it has to copy them via assignment. If we assign the value of B to A then both A and B would hold the same value. The original value of A is lost, hence we require a temporary object to hold the value of A.
Note that although languages like C++ support a native move operation, the move operation applies to resource handles only. When we swap a resource, the resources don't actually move, the only thing that changes is the "owner" of the resource. That is, it is the handle's that swap, not the resources. Even so, with move semantics we still need a temporary:
This is as close to the "perfect swap" as we can get, but it does not apply to primitive data types like numbers because the notion of "ownership" only applies to resource handles. The most efficient way to "move" a primitive data type is simply to copy it.
When we perform a swap we exchange the values of two and only two variables at a time. So to swap three values, A, B and C, we would first need to swap A with B, and then swap C with either A or B. Given that there are two possible outcomes, there is no single algorithm that can perform a three-way swap.
Using a temporary variable
temp = a
a = b
b = temp
Without a temporary variable
a = a+b
b=a-b
a = a-b
Step 1: Start
step 2: read 'a' and 'b' value
step 3: inter change the values
temp=a
a=b
b=temp
step 4: write a and b values.
step 5:stop
The algorithm to swap two numbers identified as a and b is as follows:
temp = a
a = b
b = temp
To swap two variables of type T named a and b, use the following algorithm:
T temp = a;
a = b;
b = temp;
void swap (int* a, int* b) {(*a) ^= (*b) ^= (*a) ^= (*b);
}
Compare two numbers, a and b. If a is greater than b then return a, otherwise return b. In C, we can implement this algorithm using the ternary operator (?:), as follows: return a>b?a:b;
Use a sorting algorithm. There are a bewildering number of sorting algorithms, both stable and unstable. To sort numbers, an unstable sort suffices. The algorithm you use will depend on how many numbers need to be sorted (a small or a large set), however a hybrid algorithm (a combination of two or more algorithms) can cater for both. Introsort (unstable) and timsort (stable) are the two most common hybrid sorting algorithms.
1. Design an algorithm to compute sum of the squares of n numbers?
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.
It is an algorithm used by another algorithm as part of the second algorithm's operation.As an example, an algorithm for finding the median value in a list of numbers might include sorting the numbers as a sub-algorithm: There are plenty of algorithms for sorting, and the specifics of the sorting does not matter to the "median value" algorithm, only that the numbers are sorted when the sub-algorithm is done.For what an algorithm is, see related link.
Describe an algorithm for dividing rational numbers.
Compare two numbers, a and b. If a is greater than b then return a, otherwise return b. In C, we can implement this algorithm using the ternary operator (?:), as follows: return a>b?a:b;
multiplication algorithm What?? The answer is: their product.
Shifting in easily accomplished in hardware.
Use a sorting algorithm. There are a bewildering number of sorting algorithms, both stable and unstable. To sort numbers, an unstable sort suffices. The algorithm you use will depend on how many numbers need to be sorted (a small or a large set), however a hybrid algorithm (a combination of two or more algorithms) can cater for both. Introsort (unstable) and timsort (stable) are the two most common hybrid sorting algorithms.
Yes. You know this is true because you learned a process-- an "algorithm"--for adding two numbers together, and if you start with two whole numbers, the result is also a whole number.
1. Design an algorithm to compute sum of the squares of n numbers?
You can use Euclid's algorithm to calculate the gcf of two of the numbers - then use Euclid's algorithm again with the result and the third number.Or you can factor all the numbers into prime factors, and check which prime factors occur in all three numbers.
The algorithm is A/B * C/D = AB/CD.
102
120
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.