Selection sort

How would you arrange books by height? Put the shortest on the left, then pick the shortest of the remaining books.

The best pointer remembers the position of the smallest card seen so far. The scan pointer visits the next card.

After scanning the remaining cards, swap the minimum with the first free card. Skip the swap if the minimum is already there.

Even a sorted row needs n(n − 1)/2 comparisons. This takes O(n²) time and O(1) extra space.