answersLogoWhite

0


Best Answer

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

7y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

8y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

Using a temporary variable

temp = a

a = b

b = temp

Without a temporary variable

a = a+b

b=a-b

a = a-b

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

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

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

The algorithm to swap two numbers identified as a and b is as follows:

temp = a

a = b

b = temp

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

To swap two variables of type T named a and b, use the following algorithm:

T temp = a;

a = b;

b = temp;

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

void swap (int* a, int* b) {(*a) ^= (*b) ^= (*a) ^= (*b);

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the algorithm for swapping two numbers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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.


Write algorithm compute sum of square of N numbers?

1. Design an algorithm to compute sum of the squares of n numbers?


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.

Related questions

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.


Write algorithm compute sum of square of N numbers?

1. Design an algorithm to compute sum of the squares of n numbers?


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.


All even numbers from 2-100 using algorithm?

102


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

120


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.