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

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:
2026-05-09 03:01:28 +00:00
parent a0e8b64f5c
commit 8188a82a58
3 changed files with 43 additions and 5 deletions

View File

@@ -407,6 +407,15 @@ _Newest first._
binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree *
'a tree`) with insert + in-order traversal. Tests parametric ADT,
recursive match, List.append, List.fold_left.
- 2026-05-09 Phase 6 — List.sort upgraded from O(n²) insertion sort
to O(n log n) mergesort (+3 tests, 528 total). split + merge are
inner functions of sort; tuple destructuring on the split result is
expressed via nested match (pattern parser needs explicit
paren-wrapping of tuple patterns inside match arms in some places —
inline let-tuple destructuring on a match RHS would be cleaner if
multi-binding `let (a, b) = ...` were promoted, but this works
today). Should make sort-using baselines noticeably faster on
larger lists; existing sort_uniq automatically benefits.
- 2026-05-09 Phase 4 — integer `/` is now truncate-toward-zero on
ints, IEEE on floats. Both operands integral → host floor/ceil based
on sign; otherwise host `/`. Fixes `Int.rem` (which was returning 0