Base code for the 'Swapping' algorithms.