[wpdreams_ajaxsearchpro_results id=1 element='div']

What’s a Standard Exchange?

[ad_1]

A standard swap is a programming paradigm where two variables exchange values, often used in sorting algorithms. It requires a temporary variable and can be complicated in object-oriented programming. An XOR swap technique exists but may not work in all cases.

In computer programming and computer science, a standard swap is a situation in a program where two variables must swap their values, requiring the creation of a third variable to act as an intermediary when values ​​are transferred. A standard exchange is a programming paradigm, which means that it can be applied to any situation where two pieces of data need to be exchanged, and is not tied to any data type or programming language. There are many cases in programs where values ​​need to be swapped, but it is often a standard component in collections and arrays when used with sorting algorithms that need to move elements from one index of the collection to another, using a temporary variable to contain the value to be exchanged. From a hardware perspective, a standard swap may be a supported low-level processor function for some scalar data types such as integers, meaning that the intermediate value need not be created in random access memory (RAM) to swap two numbers.

The standard swap concept can be seen with variables A and B; at the end of a standard exchange, B should equal A and A should equal B. If a program simply assigns B to A, then the value of A will be lost and cannot subsequently be assigned to B. This requires the use of a temporary variable that will hold the value of A while B is assigned to A. The final step is to assign the value of the temporary variable to B, completing the swap.

A very common application for a standard swap is when using a sorting algorithm on an array or collection. If a sorting algorithm has determined that the data in index 1 of the array should be swapped with the data in index 10, a standard data swap can be performed. This can be done exceptionally fast using pointer references in languages ​​like C.

A complication can arise in some object-oriented programming languages ​​when two object instances need to be swapped. In some very rare cases, simply exchanging object references will not initiate an actual exchange. Instead, the only way to swap objects is to clone the internal data, field by field, between the two objects and the temporary object variable.

There is a technique that can technically be used to perform a standard swap using the logical exclusive-o (XOR) operator. An XOR swap relies on the fact that, when two bits are XOR’d, they will essentially create a mask that can be inverted with either or both operators. In this way it is possible to swap two variables, A and B, by first creating a mask with an XOR of the two variables, then using the XOR to unmask the value of B first and then the value of A. Unfortunately, although this method avoids creating a temporary variable also won’t do a swap in all instances, particularly when A and B are equal to each other and in cases where compiler optimization might change the actual operation.

[ad_2]