Algorithms For Efficiently Pairing Up Socks

Really, this could be the ultimate Lifehacker topic: is there an algorithm to more effectively sort a pile of socks into pairs?

Socks picture from Shutterstock

A discussion of approaches at Stack Overflow reviews several programmer-centric ways of tackling the issue, including this neat encapsulation of a basic approach in pseudo-code:

spread_all_socks_on_flat_surface();
while (socks_left_on_a_surface()) {
// thanks to human visual SIMD, this is one, quick operation
pair = notice_any_matching_pair();
remove_socks_pair_from_surface(pair);
}

More nuanced approaches include roughly grouping socks of similar colours, then finding the matching ones, or using multiple people to sort the socks.

As with many programming problems, sometimes the most effective solution is to change the inputs. I’m one of those odd people who only ever buys socks in one style (plain black from Big W) — that way every sock matches and there’s no sorting required at all when you empty the laundry basket. Sadly, however, you still have to work out which ones are turned inside out.

How to pair socks from a pile efficiently? [Stack Overflow via Business Insider]


The Cheapest NBN 50 Plans

Here are the cheapest plans available for Australia’s most popular NBN speed tier.

At Lifehacker, we independently select and write about stuff we love and think you'll like too. We have affiliate and advertising partnerships, which means we may collect a share of sales or other compensation from the links on this page. BTW – prices are accurate and items in stock at the time of posting.

Comments


8 responses to “Algorithms For Efficiently Pairing Up Socks”