The inner keysort on your code is equivalent to:
is that what you intended?@v=((grep { $_ <= $pivot } @$v), (grep { $_ > $pivot } @$v));
I suppose that what you really want is to mimic the sort on the original post but numerically: sort first the elements > $pivot and then the rest.
Then, for your specific data, integers between -100 and 100, this works:
but if you know nothing about the numbers in $@v, then figuring a convenient sorting key can be quite difficult, it's easier to just use a grep/sort combination:my @v=nkeysort { $_<$pivot ? $_ + 200 : $_ } @$v;
that inserted on the outer sort becomes:my @v=((sort {$a<=>$b} (grep { $_ >= $pivot } @$v)), (sort {$a<=>$b} (grep { $_ < $pivot } @$v)));
BTW, note that for numeric keys you have to use nkeysort.my @sorted = nkeysort { ( ( sort {$a<=>$b} (grep { $_ >= $pivot } @$_) ), ( sort {$a<=>$b} (grep { $_ < $pivot } @$_) ) )[50] } @data;
In reply to Re^2: use Sort::Key
by salva
in thread Optimizing a sort function (wrap-around alpha)
by SineSwiper
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |