This algorithm is a simple, yet elegant solution to the age-old problem of finding the perfect sock pairing.
Or, if you're feeling particularly adventurous: