Interesting Sorting Problem

hatboy

Senior member
Oct 9, 1999
390
0
0
Here's an interesting problem: How can you sort 5 items (probably numbers or strings) using only 7 comparisons of items? I know it's possible, but I can't seem to figure out how it can be done.
 

GammaRayX

Senior member
Oct 11, 1999
282
0
0
Okay, assume the item slots are number from 1 to 5.

Then compare (and swap if needed) like so:
3 5
2 4
1 3
4 5
3 4
2 3
1 2
 

hatboy

Senior member
Oct 9, 1999
390
0
0
GammaRayX- Good try, but it doesn't work all the time. Let's say the sequence is initially 5 4 3 2 1. Then your swaps would go like this:
5 4 1 2 3
5 2 1 4 3
1 2 5 4 3
1 2 5 3 4
1 2 3 5 4
1 2 3 5 4
1 2 3 5 4

Obviously, the wrong answer. Does anybody else have any ideas?