Sorting algorithms/Quicksort: Difference between revisions
Content added Content deleted
imported>Rcmlz |
imported>Rcmlz |
||
Line 8,228: | Line 8,228: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
<syntaxhighlight lang="raku" line> |
<syntaxhighlight lang="raku" line> |
||
#| Recursive, quicksort implementation |
|||
multi quicksort(@unsorted where @unsorted.elems < 2) { @unsorted } |
|||
⚫ | |||
# Otherwise, extract first item as pivot... |
|||
my $pivot = @unsorted.pick; |
|||
⚫ | |||
my %class{Order} is default([]); |
|||
# Partition. |
|||
%class = @unsorted.classify: * cmp $pivot; |
|||
|quicksort(%class{Less}), |%class{Same}, |quicksort(%class{More}) |
|||
my $equiv := @rest.grep(* eqv $pivot); |
|||
⚫ | |||
my $after := @rest.grep(* after $pivot); |
|||
⚫ | |||
The partitions can be sorted in parallel. |
|||
# Sort the partitions. |
|||
flat quicksort($before), $pivot, $equiv, quicksort($after) |
|||
⚫ | |||
Note that <code>$before</code> and <code>$after</code> are bound to lazy lists. The partitions can be sorted in parallel. |
|||
<syntaxhighlight lang="raku" line> |
<syntaxhighlight lang="raku" line> |
||
#| |
#| Recursive, parallel, quicksort implementation |
||
multi quicksort-parallel(@unsorted where @unsorted.elems < 2) { @unsorted } |
|||
multi quicksort-parallel(@unsorted) { |
|||
* in partitioning/classifying step by .race or .hyper |
|||
my %class{Order} is default([]); |
|||
* in recursion step on Less partition by start {} |
|||
my $pivot = @unsorted.pick; |
|||
» |
|||
⚫ | |||
sub quicksort-recursive-concurrent(@input) { |
|||
⚫ | |||
return @input if @input.elems < 2; |
|||
my $ |
my $more = samewith(%class{More}); |
||
⚫ | |||
⚫ | |||
andthen |
|||
{ |
|||
my %partiton = $_; |
|||
⚫ | |||
my $more = %partiton{More}:exists ?? samewith(%partiton{More}) !! []; |
|||
⚫ | |||
⚫ | |||
} |
} |
||
#=« Implementation Notes: |
#=« Implementation Notes: |
||
* use the current thread for More partition |
|||
⚫ | |||
* hyper/race using default batch=64 and degree=4 -> tune it! |
|||
⚫ | |||
⚫ | |||
⚫ | |||
* andthen passes the result of the previous block as $_ to the next block. |
|||
» |
» |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
Line 8,283: | Line 8,272: | ||
<a 🎮 3 z 4 🐧> => <a 🎮 3 z 4 🐧>.sort, |
<a 🎮 3 z 4 🐧> => <a 🎮 3 z 4 🐧>.sort, |
||
; |
; |
||
for &quicksort, &quicksort |
for &quicksort, &quicksort-parallel -> &fun { |
||
say &fun.name; |
|||
is-deeply &fun(.key), .value, .key ~ " => " ~ .value for @testcases; |
|||
} |
} |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
<pre> |
|||
quicksort |
|||
ok 1 - => |
|||
ok 2 - a => a |
|||
ok 3 - a a => a a |
|||
ok 4 - a b => a b |
|||
ok 5 - b a => a b |
|||
ok 6 - h b a c d f e g => a b c d e f g h |
|||
ok 7 - 2 3 1 4 5 => 1 2 3 4 5 |
|||
ok 8 - a 🎮 3 z 4 🐧 => 3 4 a z 🎮 🐧 |
|||
quicksort-parallel |
|||
ok 9 - => |
|||
ok 10 - a => a |
|||
ok 11 - a a => a a |
|||
ok 12 - a b => a b |
|||
ok 13 - b a => a b |
|||
ok 14 - h b a c d f e g => a b c d e f g h |
|||
ok 15 - 2 3 1 4 5 => 1 2 3 4 5 |
|||
ok 16 - a 🎮 3 z 4 🐧 => 3 4 a z 🎮 🐧 |
|||
</pre> |
|||
=={{header|Red}}== |
=={{header|Red}}== |