diff --git a/lib/ocaml/baseline/bs_rotated.ml b/lib/ocaml/baseline/bs_rotated.ml new file mode 100644 index 00000000..87978aeb --- /dev/null +++ b/lib/ocaml/baseline/bs_rotated.ml @@ -0,0 +1,25 @@ +let bs_rotated arr target = + let lo = ref 0 in + let hi = ref (Array.length arr - 1) in + let result = ref (-1) in + while !lo <= !hi && !result = -1 do + let mid = (!lo + !hi) / 2 in + if arr.(mid) = target then result := mid + else if arr.(!lo) <= arr.(mid) then begin + if target >= arr.(!lo) && target < arr.(mid) then + hi := mid - 1 + else + lo := mid + 1 + end else begin + if target > arr.(mid) && target <= arr.(!hi) then + lo := mid + 1 + else + hi := mid - 1 + end + done; + !result + +;; + +let a = [| 4; 5; 6; 7; 0; 1; 2 |] in +bs_rotated a 0 + bs_rotated a 7 * 10 + bs_rotated a 3 * 100 diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 85242cde..a0d601ac 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -23,6 +23,7 @@ "btree.ml": 39, "brainfuck.ml": 75, "bs_bounds.ml": 3211, + "bs_rotated.ml": -66, "bsearch.ml": 7, "caesar.ml": 215, "calc.ml": 13, diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 5135c066..ae9dfdc7 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,16 @@ _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-11 Phase 5.1 — bs_rotated.ml baseline (binary search in + rotated sorted array; encoded result -66). For [4;5;6;7;0;1;2]: + - search 0 → index 4 + - search 7 → index 3 + - search 3 → −1 (not present) + Encoded: 4 + 3*10 + (-1)*100 = -66. Each step decides which half + is sorted by comparing arr[lo] vs arr[mid], then checks whether + the target falls in that sorted half. First baseline with a + negative top-level result; test runner uses literal `grep -qF` + so the leading minus is fine. 196 baseline programs total. - 2026-05-11 Phase 5.1 — task_scheduler.ml baseline (task cooldown formula, "AAABBC" with n=2 → 7 intervals). Counts each letter, finds max frequency `m` and the number of letters that hit that