Jump to content

Swap (computer programming)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Jafet (talk | contribs) at 09:04, 22 July 2006 (first draft). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
For other definitions of "swap", visit the disambiguation page.

In computer programming, the act of swapping two variables refers to mutually exchanging the values of the variables. Usually, this is done with the data in system memory. For example, in a program, two variables may be defined thus (in pseudocode):

data_item x := 1
data_item y := 0

To swap them one might do (as in programming languages like C where the swap function is built-in)

swap(x, y)

After swap() is performed, x will contain the value 0 and y will contain 1; their values have been exchanged. Of course, this operation may be generalized to other types of values, such as strings, aggregated data types and possibly entire containers.


Swap methods

Swapping data is a very important component of numerous algorithms. For example, many of the sorting algorithms, especially comparison sorts, utilize swaps to change the positions of data.


Using a temporary variable

The simplest and probably most widely used method to swap two variables is to use a third temporary variable:

define swap(x, y)
    temp = x
    x = y
    y = temp

While this is conceptually simple and in many cases the only convenient way to swap two variables, it uses extra memory. Although this should not be a problem in most applications, the sizes of the values being swapped may be huge, or the swap operation may need to be performed many times, as in sorting algorithms.

In addition, swapping two variables in object-oriented languages, such as C++ may involve one call to the class constructor and destructor for the temporary variable, and three calls to the copy constructor. Some classes may allocate memory in the constructor and deallocate it in the destructor, thus creating expensive calls to the system. Copy constructors for classes containing a lot of data, eg. in an array, may even need to copy the data manually.


XOR Swap

XOR swap uses the XOR operation to swap two numeric variables. It is generally touted to be faster than the naive method mentioned above; however it does have disadvantages. XOR swap is generally used to swap low-level data types, like integers. However, it is, in theory, capable of swapping any two values which can be represented by bitstrings.


Swap through addition and subtraction

This method swaps two variables by adding and subtracting their values. This is rarely used in practical applications, mainly because:

  • It can only swap numeric variables; it may not be possible or logical to add or subtract complex data types, like containers.
  • When swapping variables of a fixed size, arithmetic overflow becomes an issue.