What is the standard swap?

In computer programming and computer science, the standard swap situation is in a program in which two variables must be replaced, which requires that the creation of a third variable acts as an intermediary when the values ​​are transferred. A standard swap is a paradigm of programming, which means that two data can be used to replace two data and no data type or programming language is bound. There are many instances in programs where values ​​are to be replaced, but it is often a standard part in collections and fields when used with sorting algorithms that must move items from one index in the collection to another, using temporary variables to detain the exchange value. From the hardware point of view, the standard swap can be a supported processor function at low level for some scalar data types such as integers, which means that the intermediary value may not be created in random access memory (RAM) to replace two numberLa.

The concept of standard swap can be seen with variables A and B; At the end of the standard swap should be equal to A and should be equal to B. If the program simply assigns B K A, then the value A will be lost and later cannot be assigned B. This requires the use of a temporary variable that will hold the value B for a moment B is assigned.

One very common application for standard swap is using an algorithm of sorting in a field or collection. If the sorting algorithm has determined that data in the field of field 1 must be confused with data at Index 10, a standard data exchange can be performed. This can be done exceptionally quickly using links to indicators in languages ​​such as c.

In some object -oriented programming languages, complications may occur when two instances need to be replaced. In some very rare cases, it simply exchanges links to the real swap object. Instead, it is the only way to exchanget objects, clone internal data, fields by field, between two objects and variable temporary object.

There is one technique that can be technically used to perform a standard swap using an exclusive or (xor) logical operator. Xor Swap relies on the fact that when two bits with xor are calculated, they basically create a mask that can be turned with one or both operators. In this way, there may be two variables, A and B, replaced first by creating a mask with the xor of both variables, then using xor to the zashku first value for B and then the value for A. Unfortunately, even if this method avoids the creation of temporary variables, but also at all times that at all times that could be changed. could change the actual operation.

IN OTHER LANGUAGES

Was this article helpful? Thanks for the feedback Thanks for the feedback

How can we help? How can we help?