all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
|
|
@ -0,0 +1,16 @@
|
|||
procedure main() #: demonstrate various ways to sort a list and string
|
||||
demosort(bubblesort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty")
|
||||
end
|
||||
|
||||
procedure bubblesort(X,op) #: return sorted list
|
||||
local i,swapped
|
||||
|
||||
op := sortop(op,X) # select how and what we sort
|
||||
|
||||
swapped := 1
|
||||
while \swapped := &null do # the sort
|
||||
every i := 2 to *X do
|
||||
if op(X[i],X[i-1]) then
|
||||
X[i-1] :=: X[swapped := i]
|
||||
return X
|
||||
end
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
invocable all # for op
|
||||
|
||||
procedure sortop(op,X) #: select how to sort
|
||||
|
||||
op := case op of {
|
||||
"string": "<<"
|
||||
"numeric": "<"
|
||||
&null: if type(!X) == "string" then "<<" else "<"
|
||||
default: op
|
||||
}
|
||||
return proc(op, 2) | runerr(123, image(op))
|
||||
end
|
||||
|
||||
procedure cmp(a,b) #: example custom comparison procedure
|
||||
return a < b # Imagine a complex comparison test here!
|
||||
end
|
||||
|
||||
procedure demosort(sortproc,L,s) # demonstrate sort on L and s
|
||||
|
||||
write("Sorting Demo using ",image(sortproc))
|
||||
writes(" on list : ")
|
||||
writex(L)
|
||||
displaysort(sortproc,L) # default string sort
|
||||
displaysort(sortproc,L,"numeric") # explicit numeric sort
|
||||
displaysort(sortproc,L,"string") # explicit string sort
|
||||
displaysort(sortproc,L,">>") # descending string sort
|
||||
displaysort(sortproc,L,">") # descending numeric sort
|
||||
displaysort(sortproc,L,cmp) # ascending custom comparison
|
||||
displaysort(sortproc,L,"cmp") # ascending custom comparison
|
||||
|
||||
writes(" on string : ")
|
||||
writex(s)
|
||||
displaysort(sortproc,s) # sort characters in a string
|
||||
write()
|
||||
return
|
||||
end
|
||||
|
||||
procedure displaysort(sortproc,X,op) #: helper to show sort behavior
|
||||
local t,SX
|
||||
writes(" with op = ",left(image(op)||":",15))
|
||||
X := copy(X)
|
||||
t := &time
|
||||
SX := sortproc(X,op)
|
||||
writex(SX," (",&time - t," ms)")
|
||||
return
|
||||
end
|
||||
|
||||
procedure writex(X,suf[]) #: helper for displaysort
|
||||
if type(X) == "string" then
|
||||
writes(image(X))
|
||||
else {
|
||||
writes("[")
|
||||
every writes(" ",image(!X))
|
||||
writes(" ]")
|
||||
}
|
||||
every writes(!suf)
|
||||
write()
|
||||
return
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue