Sorting algorithms/Shell sort: Difference between revisions
Content added Content deleted
m (→{{header|C}}) |
(added ocaml) |
||
Line 340: | Line 340: | ||
} |
} |
||
}</lang> |
}</lang> |
||
=={{header|OCaml}}== |
|||
{{trans|C}} |
|||
<lang ocaml>exception Continue |
|||
let shellsort a = |
|||
let len = Array.length a in |
|||
let incSequence = [| 412771; 165103; 66041; 26417; 10567; |
|||
4231; 1693; 673; 269; 107; 43; 17; 7; 3; 1 |] in |
|||
for k = 0 to pred(Array.length incSequence) do |
|||
try |
|||
if (incSequence.(k) * 2) > len then raise Continue; |
|||
let increment = incSequence.(k) in |
|||
for i = increment to pred len do |
|||
let temp = a.(i) in |
|||
let rec loop j = |
|||
if j < 0 then (j) |
|||
else begin |
|||
if a.(j) <= temp then (j) |
|||
else begin |
|||
a.(j + increment) <- a.(j); |
|||
loop (j - increment) |
|||
end |
|||
end |
|||
in |
|||
let j = loop (i - increment) in |
|||
a.(j + increment) <- temp; |
|||
done; |
|||
with Continue -> () |
|||
done; |
|||
;;</lang> |
|||
and the main: |
|||
<lang ocaml>let () = |
|||
let arraysize = 1000 in (* or whatever *) |
|||
Random.self_init(); |
|||
let intArray = |
|||
Array.init arraysize (fun _ -> Random.int 4000) |
|||
in |
|||
shellsort intArray; |
|||
Array.iter (fun v -> |
|||
Printf.printf " %d" v; |
|||
) intArray; |
|||
print_newline(); |
|||
;;</lang> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |