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:
@@ -0,0 +1,27 @@
|
||||
How the JDK itself computes the midpoint (read from this JDK's lib/src.zip)
|
||||
===========================================================================
|
||||
|
||||
JDK: 25.0.5.1+2-LTS
|
||||
|
||||
java/util/Arrays.java, binarySearch0(int[] ...), lines 1698 onwards:
|
||||
1698 private static int binarySearch0(int[] a, int fromIndex, int toIndex,
|
||||
1699 int key) {
|
||||
1700 int low = fromIndex;
|
||||
1701 int high = toIndex - 1;
|
||||
1702
|
||||
1703 while (low <= high) {
|
||||
1704 int mid = (low + high) >>> 1;
|
||||
1705 int midVal = a[mid];
|
||||
1706
|
||||
1707 if (midVal < key)
|
||||
1708 low = mid + 1;
|
||||
1709 else if (midVal > key)
|
||||
1710 high = mid - 1;
|
||||
1711 else
|
||||
1712 return mid; // key found
|
||||
1713 }
|
||||
1714 return -(low + 1); // key not found.
|
||||
1715 }
|
||||
|
||||
java/util/Collections.java, indexedBinarySearch(...):
|
||||
333 int mid = (low + high) >>> 1;
|
||||
Reference in New Issue
Block a user