ocaml: phase 5.1 count_inversions.ml baseline (12 inversions via merge sort)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s

Modified merge sort that counts inversions during the merge step:
when an element from the right half is selected, the remaining
elements of the left half (mid - i + 1) all form inversions with
that right element.

  count_inv [|8; 4; 2; 1; 3; 5; 7; 6|] = 12

Inversions of [8;4;2;1;3;5;7;6]:
  with 8: (8,4)(8,2)(8,1)(8,3)(8,5)(8,7)(8,6) = 7
  with 4: (4,2)(4,1)(4,3) = 3
  with 2: (2,1) = 1
  with 7: (7,6) = 1
                                        total = 12

Tests: let rec ... and ... mutual recursion, while + ref + array
mutation, in-place sort with auxiliary scratch array.

145 baseline programs total.
This commit is contained in:
2026-05-10 05:01:08 +00:00
parent 872302ede1
commit 74d8ade089
3 changed files with 50 additions and 0 deletions

View File

@@ -407,6 +407,13 @@ _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-10 Phase 5.1 — count_inversions.ml baseline (count
inversions of [|8;4;2;1;3;5;7;6|] = 12, via merge-sort). Modified
merge sort: when right element is taken, accumulate `mid - i + 1`
inversions for the remaining left half. Tests `let rec merge ...
and sort ...` mutually recursive bindings, complex while + ref
+ array mutation, in-place sort with auxiliary array.
145 baseline programs total.
- 2026-05-10 Phase 5.1 — topo_sort.ml baseline (Kahn's algorithm
topological sort of a 6-node DAG → all 6 vertices ordered).
Standard BFS approach: compute in-degrees, seed queue with zero-