Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,37 @@
|
|||
procedure main() #: demonstrate various ways to sort a list and string
|
||||
demosort(heapsort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty")
|
||||
end
|
||||
|
||||
procedure heapsort(X,op) #: return sorted list ascending(or descending)
|
||||
local head,tail
|
||||
|
||||
op := sortop(op,X) # select how and what we sort
|
||||
|
||||
every head := (tail := *X) / 2 to 1 by -1 do # work back from from last parent node
|
||||
X := siftdown(X,op,head,tail) # sift down from head to make the heap
|
||||
|
||||
every tail := *X to 2 by -1 do { # work between the beginning and the tail to final positions
|
||||
X[1] :=: X[tail]
|
||||
X := siftdown(X,op,1,tail-1) # re-sift next (previous) branch after shortening the heap
|
||||
}
|
||||
|
||||
return X
|
||||
end
|
||||
|
||||
procedure siftdown(X,op,root,tail) #: the value @root is moved "down" the path of max(min) value to its level
|
||||
local child
|
||||
|
||||
while (child := root * 2) <= tail do { # move down the branch from root to tail
|
||||
|
||||
if op(X[child],X[tail >= child + 1]) then # choose the larger(smaller)
|
||||
child +:= 1 # ... child
|
||||
|
||||
if op(X[root],X[child]) then { # root out of order?
|
||||
X[child] :=: X[root]
|
||||
root := child # follow max(min) branch
|
||||
}
|
||||
else
|
||||
return X
|
||||
}
|
||||
return X
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue