44
55/**
66 * Binary Search Algorithm Implementation
7- *
8- * Binary search is one of the most efficient searching algorithms for finding a target
9- * element in a SORTED array. It works by repeatedly dividing the search space in half,
10- * eliminating half of the remaining elements in each step.
11- *
12- * IMPORTANT: This algorithm ONLY works correctly if the input array is sorted
13- * in ascending order.
14- *
15- * Algorithm Overview:
16- * 1. Start with the entire array (left = 0, right = array.length - 1)
17- * 2. Calculate the middle index
18- * 3. Compare the middle element with the target:
19- * - If middle element equals target: Found! Return the index
20- * - If middle element is less than target: Search the right half
21- * - If middle element is greater than target: Search the left half
22- * 4. Repeat until element is found or search space is exhausted
23- *
24- * Performance Analysis:
25- * - Best-case time complexity: O(1) - Element found at middle on first try
26- * - Average-case time complexity: O(log n) - Most common scenario
27- * - Worst-case time complexity: O(log n) - Element not found or at extreme end
28- * - Space complexity: O(1) - Only uses a constant amount of extra space
29- *
30- * Example Walkthrough:
31- * Array: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
32- * Target: 7
33- *
34- * Step 1: left=0, right=9, mid=4, array[4]=9 (9 > 7, search left half)
35- * Step 2: left=0, right=3, mid=1, array[1]=3 (3 < 7, search right half)
36- * Step 3: left=2, right=3, mid=2, array[2]=5 (5 < 7, search right half)
37- * Step 4: left=3, right=3, mid=3, array[3]=7 (Found! Return index 3)
38- *
7+ *
8+ * <p>Binary search is one of the most efficient searching algorithms for finding a target element
9+ * in a SORTED array. It works by repeatedly dividing the search space in half, eliminating half of
10+ * the remaining elements in each step.
11+ *
12+ * <p>IMPORTANT: This algorithm ONLY works correctly if the input array is sorted in ascending
13+ * order.
14+ *
15+ * <p>Algorithm Overview: 1. Start with the entire array (left = 0, right = array.length - 1) 2.
16+ * Calculate the middle index 3. Compare the middle element with the target: - If middle element
17+ * equals target: Found! Return the index - If middle element is less than target: Search the right
18+ * half - If middle element is greater than target: Search the left half 4. Repeat until element is
19+ * found or search space is exhausted
20+ *
21+ * <p>Performance Analysis: - Best-case time complexity: O(1) - Element found at middle on first
22+ * try - Average-case time complexity: O(log n) - Most common scenario - Worst-case time
23+ * complexity: O(log n) - Element not found or at extreme end - Space complexity: O(1) - Only uses
24+ * a constant amount of extra space
25+ *
26+ * <p>Example Walkthrough: Array: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] Target: 7
27+ *
28+ * <p>Step 1: left=0, right=9, mid=4, array[4]=9 (9 > 7, search left half) Step 2: left=0,
29+ * right=3, mid=1, array[1]=3 (3 < 7, search right half) Step 3: left=2, right=3, mid=2,
30+ * array[2]=5 (5 < 7, search right half) Step 4: left=3, right=3, mid=3, array[3]=7 (Found!
31+ * Return index 3)
32+ *
3933 * @author Varun Upadhyay (https://github.com/varunu28)
4034 * @author Podshivalov Nikita (https://github.com/nikitap492)
4135 * @see SearchAlgorithm
4438class BinarySearch implements SearchAlgorithm {
4539
4640 /**
47- * Generic method to perform binary search on any comparable type.
48- * This is the main entry point for binary search operations.
49- *
41+ * Generic method to perform binary search on any comparable type. This is the main entry point
42+ * for binary search operations.
43+ *
5044 * @param <T> The type of elements in the array (must be Comparable)
5145 * @param array The sorted array to search in (MUST be sorted in ascending order)
5246 * @param key The element to search for
5347 * @return The index of the key if found, -1 if not found
54- *
5548 * @throws NullPointerException if array is null
56- *
57- * Example Usage:
58- * <pre>
49+ * <p>Example Usage:
50+ * <pre>
5951 * Integer[] numbers = {1, 3, 5, 7, 9, 11};
6052 * int result = BinarySearch.find(numbers, 7);
6153 * // result will be 3 (index of element 7)
62- *
54+ *
6355 * int notFound = BinarySearch.find(numbers, 4);
6456 * // notFound will be -1 (element 4 does not exist)
6557 * </pre>
@@ -70,30 +62,27 @@ public <T extends Comparable<T>> int find(T[] array, T key) {
7062 if (array == null || array .length == 0 ) {
7163 return -1 ;
7264 }
73-
65+
7466 // Delegate to the core search implementation
7567 return search (array , key , 0 , array .length - 1 );
7668 }
7769
7870 /**
79- * Core recursive implementation of binary search algorithm.
80- * This method divides the problem into smaller subproblems recursively.
81- *
82- * How it works:
83- * 1. Calculate the middle index to avoid integer overflow
84- * 2. Check if middle element matches the target
85- * 3. If not, recursively search either left or right half
86- * 4. Base case: left > right means element not found
87- *
71+ * Core recursive implementation of binary search algorithm. This method divides the problem
72+ * into smaller subproblems recursively.
73+ *
74+ * <p>How it works: 1. Calculate the middle index to avoid integer overflow 2. Check if middle
75+ * element matches the target 3. If not, recursively search either left or right half 4. Base
76+ * case: left > right means element not found
77+ *
8878 * @param <T> The type of elements (must be Comparable)
8979 * @param array The sorted array to search in
9080 * @param key The element we're looking for
9181 * @param left The leftmost index of current search range (inclusive)
9282 * @param right The rightmost index of current search range (inclusive)
9383 * @return The index where key is located, or -1 if not found
94- *
95- * Time Complexity: O(log n) because we halve the search space each time
96- * Space Complexity: O(log n) due to recursive call stack
84+ * <p>Time Complexity: O(log n) because we halve the search space each time Space
85+ * Complexity: O(log n) due to recursive call stack
9786 */
9887 private <T extends Comparable <T >> int search (T [] array , T key , int left , int right ) {
9988 // Base case: Search space is exhausted
@@ -107,21 +96,21 @@ private <T extends Comparable<T>> int search(T[] array, T key, int left, int rig
10796 // So we use: left + (right - left) / 2 which is mathematically equivalent
10897 // but prevents overflow
10998 int median = (left + right ) >>> 1 ; // Unsigned right shift is faster division by 2
110-
99+
111100 // Get the value at middle position for comparison
112101 int comp = key .compareTo (array [median ]);
113102
114103 // Case 1: Found the target element at middle position
115104 if (comp == 0 ) {
116105 return median ; // Return the index where element was found
117- }
106+ }
118107 // Case 2: Target is smaller than middle element
119108 // This means if target exists, it must be in the LEFT half
120109 else if (comp < 0 ) {
121110 // Recursively search the left half
122111 // New search range: [left, median - 1]
123112 return search (array , key , left , median - 1 );
124- }
113+ }
125114 // Case 3: Target is greater than middle element
126115 // This means if target exists, it must be in the RIGHT half
127116 else {
@@ -130,4 +119,4 @@ else if (comp < 0) {
130119 return search (array , key , median + 1 , right );
131120 }
132121 }
133- }
122+ }
0 commit comments