Sorting algorithms/Pancake sort: Difference between revisions

Added Tailspin solution
m (→‎{{header|Phix}}: added syntax colouring the hard way)
(Added Tailspin solution)
Line 3,471:
flip(0.. 2): [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
</pre>
 
=={{header|Tailspin}}==
Simplest version, bubblesort style
<lang tailspin>
templates pancakeSort
@: {stack: $, flips: 0};
sink flip
when <2..> do
@pancakeSort.stack(1..$): $@pancakeSort.stack($..1:-1)...;
'$@pancakeSort.stack;$#10;' -> !OUT::write
@pancakeSort.flips: $@pancakeSort.flips + 1;
end flip
sink fixTop
@: 1;
2..$ -> #
$ -> \(when <~=$@fixTop> do $@fixTop -> !flip $ -> !flip \) -> !VOID
when <?($@pancakeSort.stack($) <$@pancakeSort.stack($@)..>)> do @: $;
end fixTop
$::length..2:-1 -> !fixTop
$@ !
end pancakeSort
 
[6,7,2,1,8,9,5,3,4] -> pancakeSort -> !OUT::write
</lang>
{{out}}
<pre>
[9, 8, 1, 2, 7, 6, 5, 3, 4]
[4, 3, 5, 6, 7, 2, 1, 8, 9]
[7, 6, 5, 3, 4, 2, 1, 8, 9]
[1, 2, 4, 3, 5, 6, 7, 8, 9]
[4, 2, 1, 3, 5, 6, 7, 8, 9]
[3, 1, 2, 4, 5, 6, 7, 8, 9]
[2, 1, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
{flips=8, stack=[1, 2, 3, 4, 5, 6, 7, 8, 9]}
</pre>
 
Anonymous user