Sorting algorithms/Permutation sort: Difference between revisions

Content deleted Content added
GP
Line 585:
 
1 2 3 4 5 6</lang>
 
=={{header|Perl}}==
Pass a list in by reference, and sort in situ.
<lang perl>sub psort {
my ($x, $d) = @_;
 
unless ($d //= $#$x) {
$x->[$_] < $x->[$_ - 1] and return for 1 .. $#$x;
return 1
}
for (0 .. $d) {
unshift @$x, splice @$x, $d, 1;
next if $x->[$d] < $x->[$d - 1];
return 1 if psort($x, $d - 1);
}
}
 
my @a = map+(int rand 100), 0 .. 10;
print "Before:\t@a\n";
psort(\@a);
print "After:\t@a\n"</lang>
 
Sample output:<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|PARI/GP}}==