ocaml: phase 6 List.sort + compare (+7 tests, 339 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
compare is a host builtin returning -1/0/1 (Stdlib.compare semantics) deferred to host SX </>. List.sort is insertion-sort in OCaml: O(n²) but works correctly. List.stable_sort = sort. Tested: ascending int sort, descending via custom comparator (b - a), empty list, string sort.
This commit is contained in:
@@ -143,6 +143,20 @@
|
||||
match lst with
|
||||
| [] -> None
|
||||
| (k2, v) :: t -> if k = k2 then Some v else assoc_opt k t
|
||||
|
||||
let rec sort cmp xs =
|
||||
begin
|
||||
let rec insert x ys =
|
||||
match ys with
|
||||
| [] -> [x]
|
||||
| h :: t -> if cmp x h <= 0 then x :: ys else h :: insert x t
|
||||
in
|
||||
match xs with
|
||||
| [] -> []
|
||||
| h :: t -> insert h (sort cmp t)
|
||||
end
|
||||
|
||||
let stable_sort = sort
|
||||
end ;;
|
||||
|
||||
module Option = struct
|
||||
|
||||
Reference in New Issue
Block a user