Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,46 @@
|
|||
procedure main() #: demonstrate various ways to sort a list and string
|
||||
demosort(quicksort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty")
|
||||
end
|
||||
|
||||
procedure quicksort(X,op,lower,upper) #: return sorted list
|
||||
local pivot,x
|
||||
|
||||
if /lower := 1 then { # top level call setup
|
||||
upper := *X
|
||||
op := sortop(op,X) # select how and what we sort
|
||||
}
|
||||
|
||||
if upper - lower > 0 then {
|
||||
every x := quickpartition(X,op,lower,upper) do # find a pivot and sort ...
|
||||
/pivot | X := x # ... how to return 2 values w/o a structure
|
||||
X := quicksort(X,op,lower,pivot-1) # ... left
|
||||
X := quicksort(X,op,pivot,upper) # ... right
|
||||
}
|
||||
|
||||
return X
|
||||
end
|
||||
|
||||
procedure quickpartition(X,op,lower,upper) #: quicksort partitioner helper
|
||||
local pivot
|
||||
static pivotL
|
||||
initial pivotL := list(3)
|
||||
|
||||
pivotL[1] := X[lower] # endpoints
|
||||
pivotL[2] := X[upper] # ... and
|
||||
pivotL[3] := X[lower+?(upper-lower)] # ... random midpoint
|
||||
if op(pivotL[2],pivotL[1]) then pivotL[2] :=: pivotL[1] # mini-
|
||||
if op(pivotL[3],pivotL[2]) then pivotL[3] :=: pivotL[2] # ... sort
|
||||
pivot := pivotL[2] # median is pivot
|
||||
|
||||
lower -:= 1
|
||||
upper +:= 1
|
||||
while lower < upper do { # find values on wrong side of pivot ...
|
||||
while op(pivot,X[upper -:= 1]) # ... rightmost
|
||||
while op(X[lower +:=1],pivot) # ... leftmost
|
||||
if lower < upper then # not crossed yet
|
||||
X[lower] :=: X[upper] # ... swap
|
||||
}
|
||||
|
||||
suspend lower # 1st return pivot point
|
||||
suspend X # 2nd return modified X (in case immutable)
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue