Sorting algorithms/Quicksort: Difference between revisions

Content added Content deleted
imported>Rcmlz
imported>Rcmlz
Line 8,241: Line 8,241:
flat quicksort($before), $pivot, $equiv, quicksort($after)
flat quicksort($before), $pivot, $equiv, quicksort($after)
}</syntaxhighlight>
}</syntaxhighlight>
Note that <code>$before</code> and <code>$after</code> are bound to lazy lists, so the partitions can (at least in theory) be sorted in parallel.
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.
#|« Recursive, concurrent quicksort implementation.


* in partitioning/classifying step by .race or .hyper
concurrency applied
* in partitioning step by .hyper or .race
* in recursion step on Less partition by start {}
* in recursion steps by start {}
»
»
sub quicksort-recursive-parallel(@input) {
sub quicksort-recursive-parallel(@input) {
Line 8,258: Line 8,257:
my %partiton = $_;
my %partiton = $_;
my $less = start { %partiton{Less}:exists ?? samewith(%partiton{Less}) !! [] };
my $less = start { %partiton{Less}:exists ?? samewith(%partiton{Less}) !! [] };
my $more = start { %partiton{More}:exists ?? samewith(%partiton{More}) !! [] };
my $more = %partiton{More}:exists ?? samewith(%partiton{More}) !! [];
await $less, $more;
await $less andthen |$less.result, |%partiton{Same}, |$more

|$less.result, |%partiton{Same}, |$more.result
}
}
}
}
#=« Implementation notes:
#=« Implementation Notes:
* samewith() refactors out the actual name of the routine.
* samewith() refactors out the actual name of the routine.
* andthen passes output of previous block as $_ to the next block, alternatively use ==>
* andthen passes the result of the previous block as $_ to the next block.
* no need to start a new thread for More partition - as we better use current thread.
»
»
</syntaxhighlight>
</syntaxhighlight>