Fellow ones,
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:
- perl -we "sort @ARGV" 1 2 gives Useless use of sort in void context at -e line 1
- perl -we "$x = sort @ARGV" 1 2 gives Useless use of sort in scalar context at -e line 1
So must to be used only in list context, it seems wise unless if you want to hack it: in my
Re: Perl Weekly Challenge 206 -- oneliner I used
sort to push results in
@r but, with my usual try&error, I found I need to use as return value for
sort the subtraction
$a-$b infact returning
0 failed the test with
10:10 09:30 09:00 09:55 as arguments. Using
1 as return value failed with
01:01 00:50 00:57 and returning
-1 failed the test with
10:10 09:30 09:00 09:55
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*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.