Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,41 @@
|
|||
procedure main() #: demonstrate various ways to sort a list and string
|
||||
demosort(pancakesort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty")
|
||||
pancakeflip := pancakeflipshow # replace pancakeflip procedure with a variant that displays each flip
|
||||
pancakesort([3, 14, 1, 5, 9, 2, 6, 3])
|
||||
end
|
||||
|
||||
procedure pancakesort(X,op) #: return sorted list ascending(or descending)
|
||||
local i,m
|
||||
|
||||
op := sortop(op,X) # select how and what we sort
|
||||
|
||||
every i := *X to 2 by -1 do { # work back to front
|
||||
m := 1
|
||||
every j := 2 to i do
|
||||
if op(X[m],X[j]) then m := j # find X that belongs @i high (or low)
|
||||
if i ~= m then { # not already in-place
|
||||
X := pancakeflip(X,m) # . bring max (min) to front
|
||||
X := pancakeflip(X,i) # . unsorted portion of stack
|
||||
}
|
||||
}
|
||||
return X
|
||||
end
|
||||
|
||||
procedure pancakeflip(X,tail) #: return X[1:tail] flipped
|
||||
local i
|
||||
|
||||
i := 0
|
||||
tail := integer(\tail|*X) + 1 | runerr(101,tail)
|
||||
while X[(i +:= 1) < (tail -:= 1)] :=: X[i] # flip
|
||||
return X
|
||||
end
|
||||
|
||||
procedure pancakeflipshow(X,tail) #: return X[1:tail] flipped (and display)
|
||||
local i
|
||||
|
||||
i := 0
|
||||
tail := integer(\tail|*X) + 1 | runerr(101,tail)
|
||||
while X[(i +:= 1) < (tail -:= 1)] :=: X[i] # flip
|
||||
every writes(" ["|right(!X,4)|" ]\n") # show X
|
||||
return X
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue