Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,82 @@
|
|||
module checkit {
|
||||
\\ merge sort
|
||||
group merge {
|
||||
function sort(right as stack) {
|
||||
if len(right)<=1 then =right : exit
|
||||
left=.sort(stack up right, len(right) div 2 )
|
||||
right=.sort(right)
|
||||
\\ stackitem(right) is same as stackitem(right,1)
|
||||
if stackitem(left, len(left))<=stackitem(right) then
|
||||
\\ !left take items from left for merging
|
||||
\\ so after this left and right became empty stacks
|
||||
=stack:=!left, !right
|
||||
exit
|
||||
end if
|
||||
=.merge(left, right)
|
||||
}
|
||||
function sortdown(right as stack) {
|
||||
if len(right)<=1 then =right : exit
|
||||
left=.sortdown(stack up right, len(right) div 2 )
|
||||
right=.sortdown(right)
|
||||
if stackitem(left, len(left))>stackitem(right) then
|
||||
=stack:=!left, !right : exit
|
||||
end if
|
||||
=.mergedown(left, right)
|
||||
}
|
||||
\\ left and right are pointers to stack objects
|
||||
\\ here we pass by value the pointer not the data
|
||||
function merge(left as stack, right as stack) {
|
||||
result=stack
|
||||
while len(left) > 0 and len(right) > 0
|
||||
if stackitem(left,1) <= stackitem(right) then
|
||||
result=stack:=!result, !(stack up left, 1)
|
||||
else
|
||||
result=stack:=!result, !(stack up right, 1)
|
||||
end if
|
||||
end while
|
||||
if len(right) > 0 then result=stack:= !result,!right
|
||||
if len(left) > 0 then result=stack:= !result,!left
|
||||
=result
|
||||
}
|
||||
function mergedown(left as stack, right as stack) {
|
||||
result=stack
|
||||
while len(left) > 0 and len(right) > 0
|
||||
if stackitem(left,1) > stackitem(right) then
|
||||
result=stack:=!result, !(stack up left, 1)
|
||||
else
|
||||
result=stack:=!result, !(stack up right, 1)
|
||||
end if
|
||||
end while
|
||||
if len(right) > 0 then result=stack:= !result,!right
|
||||
if len(left) > 0 then result=stack:= !result,!left
|
||||
=result
|
||||
}
|
||||
}
|
||||
k=stack:=7, 5, 2, 6, 1, 4, 2, 6, 3
|
||||
print merge.sort(k)
|
||||
print len(k)=0 ' we have to use merge.sort(stack(k)) to pass a copy of k
|
||||
|
||||
\\ input array (arr is a pointer to array)
|
||||
arr=(10,8,9,7,5,6,2,3,0,1)
|
||||
\\ stack(array pointer) return a stack with a copy of array items
|
||||
\\ array(stack pointer) return an array, empty the stack
|
||||
|
||||
arr2=array(merge.sort(stack(arr)))
|
||||
Print type$(arr2)
|
||||
Dim a()
|
||||
\\ a() is an array as a value, so we just copy arr2 to a()
|
||||
a()=arr2
|
||||
\\ to prove we add 1 to each element of arr2
|
||||
arr2++
|
||||
Print a() ' 0,1,2,3,4,5,6,7,8,9
|
||||
Print arr2 ' 1,2,3,4,5,6,7,8,9,11
|
||||
p=a() ' we get a pointer
|
||||
\\ a() has a double pointer inside
|
||||
\\ so a() get just the inner pointer
|
||||
a()=array(merge.sortdown(stack(p)))
|
||||
\\ so now p (which use the outer pointer)
|
||||
\\ still points to a()
|
||||
print p ' p point to a()
|
||||
|
||||
}
|
||||
checkit
|
||||
Loading…
Add table
Add a link
Reference in a new issue