Jump to content

Sorting algorithms/Permutation sort: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(→‎{{header|Python}}: Added cleaner Python 3 version, with more_itertools.window)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 90:
While i < j
t := %v%%i%, %v%%i% := %v%%j%, %v%%j% := t, ++i, --j
}</lang>
 
=={{header|BBC BASIC}}==
Line 517:
 
→ (0 1 2 3 4 5)
</lang>
 
=={{header|Elixir}}==
Line 1,290:
<pre>Before: 94 15 42 35 55 24 96 14 61 94 43
After: 14 15 24 35 42 43 55 61 94 94 96</pre>
 
=={{header|Perl 6}}==
<lang perl6># Lexicographic permuter from "Permutations" task.
sub next_perm ( @a ) {
my $j = @a.end - 1;
$j-- while $j >= 1 and [>] @a[ $j, $j+1 ];
 
my $aj = @a[$j];
my $k = @a.end;
$k-- while [>] $aj, @a[$k];
 
@a[ $j, $k ] .= reverse;
 
my Int $r = @a.end;
my Int $s = $j + 1;
while $r > $s {
@a[ $r, $s ] .= reverse;
$r--;
$s++;
}
 
sub permutation_sort ( @a ) {
my @n = @a.keys;
my $perm_count = [*] 1 .. +@n; # Factorial
for ^$perm_count {
my @permuted_a = @a[ @n ];
return @permuted_a if [le] @permuted_a;
next_perm(@n);
}
 
my @data = < c b e d a >; # Halfway between abcde and edcba
say 'Input = ' ~ @data;
say 'Output = ' ~ @data.&permutation_sort;
</lang>
 
{{out}}
<pre>Input = c b e d a
Output = a b c d e</pre>
 
=={{header|Phix}}==
Line 1,578 ⟶ 1,538:
(sort '(6 1 5 2 4 3)) ; => '(1 2 3 4 5 6)
</lang>
 
=={{header|Perl 6Raku}}==
(formerly Perl 6)
<lang perl6># Lexicographic permuter from "Permutations" task.
sub next_perm ( @a ) {
my $j = @a.end - 1;
$j-- while $j >= 1 and [>] @a[ $j, $j+1 ];
 
my $aj = @a[$j];
my $k = @a.end;
$k-- while [>] $aj, @a[$k];
 
@a[ $j, $k ] .= reverse;
 
my Int $r = @a.end;
my Int $s = $j + 1;
while $r > $s {
@a[ $r, $s ] .= reverse;
$r--;
$s++;
}
 
sub permutation_sort ( @a ) {
my @n = @a.keys;
my $perm_count = [*] 1 .. +@n; # Factorial
for ^$perm_count {
my @permuted_a = @a[ @n ];
return @permuted_a if [le] @permuted_a;
next_perm(@n);
}
 
my @data = < c b e d a >; # Halfway between abcde and edcba
say 'Input = ' ~ @data;
say 'Output = ' ~ @data.&permutation_sort;
</lang>
 
{{out}}
<pre>Input = c b e d a
Output = a b c d e</pre>
 
=={{header|REXX}}==
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.