Add binary-search module: iterative/recursive search, overflow-safe midpoint, lowerBound/upperBound, rotated arrays, search on the answer, 10 interview problems (14 tests, 14 transcripts)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b3tGdV3Vm51QUZxg2x
This commit is contained in:
Claude
2026-11-22 19:47:36 +05:30
parent 3336e2ba15
commit ce12a80f58
39 changed files with 2110 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
Rotated sorted array [4, 5, 6, 7, 0, 1, 2]
==========================================
search for 0
low=0 high=6 mid=3 a[mid]=7 | left half 4..7 is sorted and does not hold 0 -> low=4
low=4 high=6 mid=5 a[mid]=1 | left half 0..1 is sorted and holds 0 -> high=4
low=4 high=4 mid=4 a[mid]=0 -> found at 4
search for 3
low=0 high=6 mid=3 a[mid]=7 | left half 4..7 is sorted and does not hold 3 -> low=4
low=4 high=6 mid=5 a[mid]=1 | left half 0..1 is sorted and does not hold 3 -> low=6
low=6 high=6 mid=6 a[mid]=2 | left half 2..2 is sorted and does not hold 3 -> low=7
low=7 high=6 -> low > high, return -1
find the minimum (the rotation point)
low=0 high=6 mid=3 a[mid]=7 a[high]=2 -> 7 > 2, the drop is right of mid, low=4
low=4 high=6 mid=5 a[mid]=1 a[high]=2 -> 1 <= 2, the drop is at mid or left of it, high=5
low=4 high=5 mid=4 a[mid]=0 a[high]=1 -> 0 <= 1, the drop is at mid or left of it, high=4
low=4 high=4 -> minimum 0 at index 4
exhaustive check, every rotation of every distinct array of length 1..12, every key: 2184 searches, 0 disagreements
With duplicates the same idea can degrade to a linear scan (iterations of the find-minimum loop):
n= 16 minimum at index 1: distinct values 4 iterations many duplicates 14 iterations
n= 256 minimum at index 1: distinct values 8 iterations many duplicates 254 iterations
n= 4096 minimum at index 1: distinct values 12 iterations many duplicates 4094 iterations