Standard swap

In computer science, the standard swap or three point turn is a programming paradigm that exchanges the values of two variables.

By using a third variable, whose value is otherwise of no concern, to hold the value of the first, and then assigning the second's to the first, and the third's back to the second, the values of the first two are swapped.

For example:

  1. let temp = b
  2. let b = a
  3. let a = temp

In modern CPUs this is accomplished on the processor itself, in a single machine instruction, rather than having to go through RAM.


This article is issued from Wikipedia - version of the Wednesday, January 29, 2014. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.