Sorting algorithms/Insertion sort: Difference between revisions
Content deleted Content added
Not a robot (talk | contribs) Add CLU |
|||
Line 1,117: | Line 1,117: | ||
;Usage:(in-sort! [6,8,5,9,3,2,1,4,7]) |
;Usage:(in-sort! [6,8,5,9,3,2,1,4,7]) |
||
;Returns: [1 2 3 4 5 6 7 8 9]</lang> |
;Returns: [1 2 3 4 5 6 7 8 9]</lang> |
||
=={{header|CLU}}== |
|||
<lang clu>% 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</lang> |
|||
{{out}} |
|||
<pre>Before: 7 -5 0 2 99 16 4 20 47 19 |
|||
After: -5 0 2 4 7 16 19 20 47 99</pre> |
|||
=={{header|CMake}}== |
=={{header|CMake}}== |