Sorting algorithms/Pancake sort: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: changed whitespace and comments, simplified some code.) |
m (→{{header|REXX}}: added an optimization.) |
||
Line 3,137: | Line 3,137: | ||
exit 0 /*stick a fork in it, we're all done. */ |
exit 0 /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
inOrder: parse arg n; do j=1 for n-1; k= j+1; if @.j>@.k then return 0; end; return 1 |
|||
panFlip: parse arg y; do i=1 for (y+1)%2; yi=y-i+1; _=@.i; @.i=@.yi; @.yi=_; end; return |
|||
show: do k=1 for #; say @element right(k,length(#)) arg(1)':' right(@.k,9); end; return |
show: do k=1 for #; say @element right(k,length(#)) arg(1)':' right(@.k,9); end; return |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
Line 3,148: | Line 3,149: | ||
bp=2 17 5 29 7 37 13 61 43 181 47 197 67 277 97 397 113 461 137 557 167 677 173 701, |
bp=2 17 5 29 7 37 13 61 43 181 47 197 67 277 97 397 113 461 137 557 167 677 173 701, |
||
797 1117 307 1237 1597 463 1861 467 |
797 1117 307 1237 1597 463 1861 467 |
||
$=bp fibs; |
$= bp fibs; #= words($) /*combine the two lists; get # of items*/ |
||
do j=1 for #; @.j= word($, j); end /*◄─── obtain a number from the $ list.*/ |
|||
return /* [↑] populate the @. array with #s*/ |
return /* [↑] populate the @. array with #s*/ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
pancakeSort: procedure expose @.; parse arg n . |
pancakeSort: procedure expose @.; parse arg n .; if inOrder(n) then return |
||
do n=n by -1 for n-1 |
do n=n by -1 for n-1 |
||
!= @.1; ?= 1; do j=2 to n; |
!= @.1; ?= 1; do j=2 to n; if @.j<=! then iterate |
||
!= @.j; |
!= @.j; ?= j |
||
end /*j*/ |
end /*j*/ |
||
call |
call panFlip ?; call panFlip n |
||
end /*n*/; return</lang> |
end /*n*/; return</lang> |
||
{{out|output|text= when using the internally generated numbers:}} |
{{out|output|text= when using the internally generated numbers:}} |
||