after so many years I still lack the understanding of basics.
I always thought of sort as if it should check every element of a list against any other element: this is not true. Infact rereading the sort docs, the very bottom line, I read: is implemented via the mergesort algorithm
Mergesort is based on old Latin motto divide et impera and because of this not every combination is take in count while sorting. It is optimized to compute only the lowest number of checks.
Let clean the field: sort is not like map infact:
Why?
In the docs I read also subroutine that returns an integer less than, equal to, or greater than 0 so I tried to see what happens forcing the returned value:
perl -le "@a = sort{ print qq($a $b); 0 }@ARGV" 1 2 3 4 1 2 3 4 1 3 3 2 perl -le "@a = sort{ print qq($a $b); 1 }@ARGV" 1 2 3 4 1 2 3 4 2 4 2 3 perl -le "@a = sort{ print qq($a $b); -1 }@ARGV" 1 2 3 4 1 2 3 4 1 3 3 2 2 4 # one iterati +on more with -1
In the last example there is an iteration more: why? I'm fooling the algorithm telling $b is always less than $a or what?
Also the only missing couple frome the last example is 1 4 can I force sort to be unoptimized as I wanted in my oneliner example?
Beside Very nice title but: hey kid! Stop messing with sort code! do you have something wise to share?
L*
|
---|