I came out with another solution which would not promise fewer operations, neither less time consumption, but it should be tried to see if it can be a good-enough heuristic to provide less time consumption in huge series of sock pairing.
Preconditions: There is no guarantee that there are the same socks. If they are of the same color it doesn't mean they have the same size or pattern. Socks are randomly shuffled. There can be odd number of socks (some are missing, we don't know how many). Prepare to remember a variable "index" and set it to 0.
The result will have one or two piles: 1. "matched" and 2. "missing"
Heuristic:
- Find most distinctive sock.
- Find its match.
- If there is no match, put it on the "missing" pile.
- Repeat from 1. until there are no more most distinctive socks.
- If there are less then 6 socks, go to 11.
- Pair blindly all socks to its neighbor (do not pack it)
- Find all matched pairs, pack it and move packed pairs to "matched" pile; If there were no new matches - increment "index" by 1
- If "index" is greater then 2 (this could be value dependent on sock number because with greater number of socks there are less chance to pair them blindly) go to 11
- Shuffle the rest
- Go to 1
- Forget "index"
- Pick a sock
- Find its pair
- If there is no pair for the sock, move it to the "missing" pile
- If match found pair it, pack pair and move it to the "matched" pile
- If there are still more then one socks go to 12
- If there is just one left go to 14
- Smile satisfied :)
Also, there could be added check for damaged socks also, as if the removal of those. It could be inserted between 2 and 3, and between 13 and 14.
I have never tried this and amI'm looking forward to hear about any experiences or corrections.