Sorting algorithms/Pancake sort: Difference between revisions
Content added Content deleted
Line 2,406: | Line 2,406: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
<lang Mathematica>ClearAll[LMaxPosition, Flip, pancakeSort] |
<lang Mathematica>ClearAll[LMaxPosition, Flip, pancakeSort] |
||
LMaxPosition[a_, n_] := |
LMaxPosition[a_, n_] := With[{b = Take[a, n]}, First[Ordering[b, -1]]] |
||
With[{b = Take[a, n]}, First[Ordering[b, -1]]] |
|||
SetAttributes[Flip, HoldAll]; |
SetAttributes[Flip, HoldAll]; |
||
Flip[a_] := Set[a, Reverse[a]] |
Flip[a_] := Set[a, Reverse[a]] |
||
pancakeSort[in_] := Module[{n, lm, a = in}, |
pancakeSort[in_] := Module[{n, lm, a = in, flips = 0}, |
||
Do[ |
|||
⚫ | |||
lm = LMaxPosition[a, n]; |
lm = LMaxPosition[a, n]; |
||
If[lm < n, |
If[lm < n, |
||
Flip[a[[;; lm]]]; |
Flip[a[[;; lm]]]; |
||
Print[a]; |
|||
Flip[a[[;; n]]]; |
Flip[a[[;; n]]]; |
||
]; |
|||
, |
|||
⚫ | |||
⚫ | |||
] |
]; |
||
⚫ | |||
] |
|||
pancakeSort[{6, 7, 8, 9, 2, 5, 3, 4, 1}]</lang> |
pancakeSort[{6, 7, 8, 9, 2, 5, 3, 4, 1}]</lang> |
||
{{out}} |
{{out}} |