Skip to content

Commit 36ac67b

Browse files
ghorpadeireclaude
andcommitted
fix: correct Javadoc formatting and add missing newline at EOF
- Fix Javadoc structure with proper tag ordering (description before @params) - Remove incorrect @throws tag (method returns -1, doesn't throw) - Format algorithm steps as proper HTML ordered list - Move complexity analysis before @param tags - Add missing newline at end of file - Fix example code to use instance method call Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 8c851ff commit 36ac67b

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

src/main/java/com/thealgorithms/searches/BinarySearch.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,20 @@ class BinarySearch implements SearchAlgorithm {
4141
* Generic method to perform binary search on any comparable type. This is the main entry point
4242
* for binary search operations.
4343
*
44-
* @param <T> The type of elements in the array (must be Comparable)
45-
* @param array The sorted array to search in (MUST be sorted in ascending order)
46-
* @param key The element to search for
47-
* @return The index of the key if found, -1 if not found
48-
* @throws NullPointerException if array is null
49-
* <p>Example Usage:
50-
* <pre>
44+
* <p>Example Usage:
45+
* <pre>
5146
* Integer[] numbers = {1, 3, 5, 7, 9, 11};
52-
* int result = BinarySearch.find(numbers, 7);
47+
* int result = new BinarySearch().find(numbers, 7);
5348
* // result will be 3 (index of element 7)
5449
*
55-
* int notFound = BinarySearch.find(numbers, 4);
50+
* int notFound = new BinarySearch().find(numbers, 4);
5651
* // notFound will be -1 (element 4 does not exist)
5752
* </pre>
53+
*
54+
* @param <T> The type of elements in the array (must be Comparable)
55+
* @param array The sorted array to search in (MUST be sorted in ascending order)
56+
* @param key The element to search for
57+
* @return The index of the key if found, -1 if not found or if array is null/empty
5858
*/
5959
@Override
6060
public <T extends Comparable<T>> int find(T[] array, T key) {
@@ -71,18 +71,23 @@ public <T extends Comparable<T>> int find(T[] array, T key) {
7171
* Core recursive implementation of binary search algorithm. This method divides the problem
7272
* into smaller subproblems recursively.
7373
*
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 &gt; right means element not found
74+
* <p>How it works:
75+
* <ol>
76+
* <li>Calculate the middle index to avoid integer overflow</li>
77+
* <li>Check if middle element matches the target</li>
78+
* <li>If not, recursively search either left or right half</li>
79+
* <li>Base case: left &gt; right means element not found</li>
80+
* </ol>
81+
*
82+
* <p>Time Complexity: O(log n) because we halve the search space each time.
83+
* Space Complexity: O(log n) due to recursive call stack.
7784
*
7885
* @param <T> The type of elements (must be Comparable)
7986
* @param array The sorted array to search in
8087
* @param key The element we're looking for
8188
* @param left The leftmost index of current search range (inclusive)
8289
* @param right The rightmost index of current search range (inclusive)
8390
* @return The index where key is located, or -1 if not found
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
8691
*/
8792
private <T extends Comparable<T>> int search(T[] array, T key, int left, int right) {
8893
// Base case: Search space is exhausted
@@ -119,4 +124,4 @@ else if (comp < 0) {
119124
return search(array, key, median + 1, right);
120125
}
121126
}
122-
}
127+
}

0 commit comments

Comments
 (0)