answersLogoWhite

0

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:

  1. Copy the value of A to T
  2. Copy the value of B to A
  3. Copy the value of T to B

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:

  1. Move ownership of A's resource to T
  2. Move ownership of B's resource to A
  3. Move ownership of T's resource to B

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.

User Avatar

Wiki User

8y ago

What else can I help you with?

Continue Learning about Engineering

What is the algorithm for determining the maximum of two 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;


How do you sort an array of numbers?

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.


How do you write a javascript to swap two numbers without using third one?

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.


What is sub-algorithm?

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.


What is algorithm in basic computer science?

An algorithm is a set of instructions that a computer follows, generally to accomplish one specific task. These tasks can range from sorting a set of numbers to finding the greatest common denominator of two numbers.

Related Questions

What is the time complexity of the Karatsuba algorithm for multiplying two numbers?

The time complexity of the Karatsuba algorithm for multiplying two numbers is O(nlog2(3)), which is approximately O(n1.585).


How do you describe an algorithm with rational numbers?

Describe an algorithm for dividing rational numbers.


What is the algorithm for determining the maximum of two 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;


The result obtained when two or more numbers are mutiplied is called a what?

multiplication algorithm What?? The answer is: their product.


Why is booth algorithm fast for multiplication two binary numbers?

Shifting in easily accomplished in hardware.


How do you sort an array of numbers?

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.


Is the sum of two natural numbers always a natural number?

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.


What is the GCF of 180 336 and 504?

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.


What is an algorithm for multiplying rational numbers?

The algorithm is A/B * C/D = AB/CD.


How many combinations are there in 5 numbers by swapping the way they are written?

120


All even numbers from 2-100 using algorithm?

102


C program to swapping two numbers using call by value method?

You have to pass the address of the variables.void swap (int *pa, int *pb){...}