in reply to sort with fewest moves
Interestingly enough, there seems to be scant details available on the net. Google doesn't turn up much. I read about this technique in a long-lost issue of Computer Language.
The only link halfway useful that I have found is www.cs.brandeis.edu/~hugues/sorting_networks.html ... I think you're going to have to dig out a copy of Knuth volume III - Searching and Sorting. <update> Hmm, I just did. The Knuth, as usual, is heavy on mathematics and short on algorithms. I still can't find any code to help you. It's in section 5.3.1 (Minimum-comparison sorting) for what it's worth.</update>
The algorithm essentially accepts a single number (the number of elements to sort), and then spits out a series of pairs, which are the indices of the elements to compare. And it turns out that as some of the comparison (a,b) and (c,d) don't affect each other, you can run them in parallel, thereby reducing the overall time taken.
It is apparently very hard to generate a minimal number of comparisons. These days people are attacking the problem through Genetic Algorithm (GA) techniques.
Some more links. Using sorting networks reveals more hits.
- cs.engr.uky.edu/~lewis/essays/algorithms/sortnets/sort-net.html
- www.theory.csc.uvic.ca/~mmania/networks/intro.htm
print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'