Anonymous user
Sorting algorithms/Quicksort: Difference between revisions
→{{header|Raku}}
imported>Rcmlz |
imported>Rcmlz |
||
Line 8,228:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>
multi quicksort(@unsorted where @unsorted.elems < 2) { @unsorted }
my $pivot = @unsorted.pick;
▲ multi quicksort([$pivot, *@rest]) {
my %class{Order} is default([]);
%class
|quicksort(%class{Less}), |%class{Same}, |quicksort(%class{More})
The partitions can be sorted in parallel.
▲}</syntaxhighlight>
<syntaxhighlight lang="raku" line>
#|
multi quicksort-parallel(@unsorted where @unsorted.elems < 2) { @unsorted }
multi quicksort-parallel(@unsorted) {
my %class{Order} is default([]);
my $pivot = @unsorted.pick;
my $
▲ @input.race.classify( * cmp $pivot )
▲ my $less = start { %partiton{Less}:exists ?? samewith(%partiton{Less}) !! [] };
▲ await $less andthen |$less.result, |%partiton{Same}, |$more
▲ }
}
#=« Implementation Notes:
▲ * recursion on the Less partition can create a large number of new threads -> limit it!
▲ * samewith() refactors out the actual name of the routine.
»
</syntaxhighlight>
Line 8,283 ⟶ 8,272:
<a 🎮 3 z 4 🐧> => <a 🎮 3 z 4 🐧>.sort,
;
for &quicksort, &quicksort
say &fun.name;
}
</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}}==
|