Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,36 @@
|
|||
% Insertion-sort an array in place.
|
||||
insertion_sort = proc [T: type] (a: array[T])
|
||||
where T has lt: proctype (T,T) returns (bool)
|
||||
|
||||
bound_lo: int := array[T]$low(a)
|
||||
bound_hi: int := array[T]$high(a)
|
||||
|
||||
for i: int in int$from_to(bound_lo, bound_hi) do
|
||||
value: T := a[i]
|
||||
j: int := i - 1
|
||||
while j >= bound_lo cand value < a[j] do
|
||||
a[j+1] := a[j]
|
||||
j := j-1
|
||||
end
|
||||
a[j+1] := value
|
||||
end
|
||||
end insertion_sort
|
||||
|
||||
% Print an array
|
||||
print_arr = proc [T: type] (a: array[T], w: int, s: stream)
|
||||
where T has unparse: proctype (T) returns (string)
|
||||
for el: T in array[T]$elements(a) do
|
||||
stream$putright(s, T$unparse(el), w)
|
||||
end
|
||||
stream$putc(s, '\n')
|
||||
end print_arr
|
||||
|
||||
start_up = proc ()
|
||||
ai = array[int]
|
||||
po: stream := stream$primary_output()
|
||||
test: ai := ai$[7, -5, 0, 2, 99, 16, 4, 20, 47, 19]
|
||||
|
||||
stream$puts(po, "Before: ") print_arr[int](test, 3, po)
|
||||
insertion_sort[int](test)
|
||||
stream$puts(po, "After: ") print_arr[int](test, 3, po)
|
||||
end start_up
|
||||
Loading…
Add table
Add a link
Reference in a new issue