ocaml: phase 6 List.sort upgraded to mergesort (+3 tests, 528 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
The previous List.sort was O(n^2) insertion sort. Replaced with a
straightforward mergesort:
split lst -> alternating-take into ([odd], [even])
merge xs ys -> classic two-finger merge under cmp
sort cmp xs -> base cases [], [x]; otherwise split + recursive
sort on each half + merge
Tuple destructuring on the split result is expressed via nested
match — let-tuple-destructuring would be cleaner but works today.
This benefits sort_uniq (which calls sort first), Set.Make.add via
sort etc., and any user program using List.sort. Stable_sort is
already aliased to sort.
This commit is contained in:
@@ -146,14 +146,30 @@
|
||||
|
||||
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
|
||||
let rec split lst =
|
||||
match lst with
|
||||
| [] -> ([], [])
|
||||
| [x] -> ([x], [])
|
||||
| x :: y :: rest ->
|
||||
(match split rest with
|
||||
| (a, b) -> (x :: a, y :: b))
|
||||
in
|
||||
let rec merge xs ys =
|
||||
match xs with
|
||||
| [] -> ys
|
||||
| x :: xs' ->
|
||||
(match ys with
|
||||
| [] -> xs
|
||||
| y :: ys' ->
|
||||
if cmp x y <= 0 then x :: merge xs' (y :: ys')
|
||||
else y :: merge (x :: xs') ys')
|
||||
in
|
||||
match xs with
|
||||
| [] -> []
|
||||
| h :: t -> insert h (sort cmp t)
|
||||
| [x] -> [x]
|
||||
| _ ->
|
||||
(match split xs with
|
||||
| (a, b) -> merge (sort cmp a) (sort cmp b))
|
||||
end
|
||||
|
||||
let stable_sort = sort
|
||||
|
||||
@@ -1310,6 +1310,14 @@ cat > "$TMPFILE" << 'EPOCHS'
|
||||
(epoch 5054)
|
||||
(eval "(ocaml-run \"max_int + min_int\")")
|
||||
|
||||
;; ── List.sort mergesort ──────────────────────────────────────
|
||||
(epoch 5060)
|
||||
(eval "(ocaml-run \"List.sort compare [5;2;8;1;9;3;7;4;6]\")")
|
||||
(epoch 5061)
|
||||
(eval "(ocaml-run \"List.sort (fun a b -> b - a) [3;1;4;1;5]\")")
|
||||
(epoch 5062)
|
||||
(eval "(ocaml-run \"List.length (List.sort compare [9;8;7;6;5;4;3;2;1;0])\")")
|
||||
|
||||
EPOCHS
|
||||
|
||||
OUTPUT=$(timeout 360 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
|
||||
@@ -2081,6 +2089,11 @@ check 5052 "Int.rem 17 5" '2'
|
||||
check 5053 "Int.compare 5 3" '1'
|
||||
check 5054 "max_int + min_int (host int)" '0'
|
||||
|
||||
# ── List.sort mergesort ─────────────────────────────────────────
|
||||
check 5060 "sort 9-element list" '(1 2 3 4 5 6 7 8 9)'
|
||||
check 5061 "sort with reverse cmp" '(5 4 3 1 1)'
|
||||
check 5062 "sort 10 reversed -> length" '10'
|
||||
|
||||
TOTAL=$((PASS + FAIL))
|
||||
if [ $FAIL -eq 0 ]; then
|
||||
echo "ok $PASS/$TOTAL OCaml-on-SX tests passed"
|
||||
|
||||
Reference in New Issue
Block a user