2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,5 +1,9 @@
|
|||
{{Sorting Algorithm}}
|
||||
Sort an array of integers (of any convenient size) into ascending order using [[wp:Pancake sorting|Pancake sorting]]. In short, instead of individual elements being sorted, the only operation allowed is to "flip" one end of the list, like so:
|
||||
|
||||
;Task:
|
||||
Sort an array of integers (of any convenient size) into ascending order using [[wp:Pancake sorting|Pancake sorting]].
|
||||
|
||||
In short, instead of individual elements being sorted, the only operation allowed is to "flip" one end of the list, like so:
|
||||
Before:
|
||||
'''6 7 8 9''' 2 5 3 4 1
|
||||
After:
|
||||
|
|
@ -14,3 +18,4 @@ For more information on pancake sorting, see [[wp:Pancake sorting|the Wikipedia
|
|||
See also:
|
||||
* [[Number reversal game]]
|
||||
* [[Topswops]]
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,65 @@
|
|||
-- Initialisation
|
||||
math.randomseed(os.time())
|
||||
numList = {step = 0, sorted = 0}
|
||||
|
||||
-- Create list of n random values
|
||||
function numList:build (n)
|
||||
self.values = {}
|
||||
for i = 1, n do self.values[i] = math.random(-100, 100) end
|
||||
end
|
||||
|
||||
-- Return boolean indicating whether the list is in order
|
||||
function numList:isSorted ()
|
||||
for i = 2, #self.values do
|
||||
if self.values[i] < self.values[i - 1] then return false end
|
||||
end
|
||||
print("Finished!")
|
||||
return true
|
||||
end
|
||||
|
||||
-- Display list of numbers on one line
|
||||
function numList:show ()
|
||||
if self.step == 0 then
|
||||
io.write("Initial state:\t")
|
||||
else
|
||||
io.write("After step " .. self.step .. ":\t")
|
||||
end
|
||||
for _, v in ipairs(self.values) do io.write(v .. " ") end
|
||||
print()
|
||||
end
|
||||
|
||||
-- Reverse n values from the left
|
||||
function numList:reverse (n)
|
||||
local flipped = {}
|
||||
for i, v in ipairs(self.values) do
|
||||
if i > n then
|
||||
flipped[i] = v
|
||||
else
|
||||
flipped[i] = self.values[n + 1 - i]
|
||||
end
|
||||
end
|
||||
self.values = flipped
|
||||
end
|
||||
|
||||
-- Perform one flip of a pancake sort
|
||||
function numList:pancake ()
|
||||
local maxPos = 1
|
||||
for i = 1, #self.values - self.sorted do
|
||||
if self.values[i] > self.values[maxPos] then maxPos = i end
|
||||
end
|
||||
if maxPos == 1 then
|
||||
numList:reverse(#self.values - self.sorted)
|
||||
self.sorted = self.sorted + 1
|
||||
else
|
||||
numList:reverse(maxPos)
|
||||
end
|
||||
self.step = self.step + 1
|
||||
end
|
||||
|
||||
-- Main procedure
|
||||
numList:build(10)
|
||||
numList:show()
|
||||
repeat
|
||||
numList:pancake()
|
||||
numList:show()
|
||||
until numList:isSorted()
|
||||
|
|
@ -1,35 +1,32 @@
|
|||
/*REXX program sorts and displays an array using the pancake sort algorithm.*/
|
||||
call gen /*generate elements in the @. array.*/
|
||||
call show 'before sort' /*display the BEFORE array elements.*/
|
||||
say copies('▒',40) /*display a separator line for eyeballs*/
|
||||
call pancakeSort # /*invoke the pancake sort. Yummy. */
|
||||
call show ' after sort' /*display the AFTER array elements. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
flip: procedure expose @.; parse arg y
|
||||
do i=1 for (y+1)%2; ymp=y-i+1; _=@.i; @.i=@.ymp; @.ymp=_
|
||||
end /*i*/
|
||||
return
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
gen: fibs= '-55 -21 -1 -8 -8 -21 -55 0 0' /*some non─positive Fibonacci numbers, most of which are repeated. */
|
||||
/* ┌───◄ a few sorted bread primes which are primes of the form: (p-3)÷2 and 2∙p+3 */
|
||||
/* ↓ where p is a prime. Bread primes are related to sandwich and meat primes.*/
|
||||
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
|
||||
$=bp fibs; #=words($) /*combine the two lists; get # of items*/
|
||||
/* [↓] populate the @. array with #s*/
|
||||
do j=1 for #; @.j=word($,j); end /*◄─── obtain a number from the $ list.*/
|
||||
return
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
/*REXX program sorts and displays an array using the pancake sort algorithm. */
|
||||
call gen /*generate elements in the @. array.*/
|
||||
call show 'before sort' /*display the BEFORE array elements.*/
|
||||
say copies('▒', 60) /*display a separator line for eyeballs*/
|
||||
call pancakeSort # /*invoke the pancake sort. Yummy. */
|
||||
call show ' after sort' /*display the AFTER array elements. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
flip: parse arg y; do i=1 for (y+1)%2; yyy=y-i+1; _=@.i; @.i=@.yyy; @.yyy=_; end
|
||||
return /*swap: ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
gen: fibs= '-55 -21 -1 -8 -8 -21 -55 0 0' /*some non─positive Fibonacci numbers, */
|
||||
@element=right('element',21) /* most of which are repeated.
|
||||
┌◄─┬──◄─ some paired bread primes which are of the form: (p-3)÷2 and 2∙p+3
|
||||
│ │ where p is a prime. Bread primes are related to sandwich & meat primes.
|
||||
↓ ↓ ──── ──── ───── ────── ────── ────── ────── ─────── ─────── ─────── ──────*/
|
||||
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
|
||||
$=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*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
pancakeSort: procedure expose @.; parse arg N
|
||||
|
||||
do N=N by -1 for N-1
|
||||
!=@.1; ?=1; do j=2 to N; if @.j<=! then iterate
|
||||
!=@.j; ?=j
|
||||
end /*j*/
|
||||
call flip ?; call flip N
|
||||
end /*N*/
|
||||
return
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
show: w=length(#) /* [↓] display elements of @. array.*/
|
||||
do k=1 for #; say ' element' right(k,w) arg(1)':' right(@.k,9); end
|
||||
return
|
||||
do N=N by -1 for N-1
|
||||
!=@.1; ?=1; do j=2 to N; if @.j<=! then iterate
|
||||
!=@.j; ?=j
|
||||
end /*j*/
|
||||
call flip ?; call flip N
|
||||
end /*N*/
|
||||
return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
show: do k=1 for #; say @element right(k,length(#)) arg(1)':' right(@.k,9); end; return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue