Skip to content

Commit 1dc64b1

Browse files
authored
Update BinarySearch (#4747)
1 parent e9bbf35 commit 1dc64b1

File tree

2 files changed

+87
-18
lines changed

2 files changed

+87
-18
lines changed
Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,54 @@
11
package com.thealgorithms.searches;
22

3-
class PerfectBinarySearch {
3+
import com.thealgorithms.devutils.searches.SearchAlgorithm;
44

5-
static int binarySearch(int[] arr, int target) {
6-
int low = 0;
7-
int high = arr.length - 1;
5+
/**
6+
* Binary search is one of the most popular algorithms The algorithm finds the
7+
* position of a target value within a sorted array
8+
*
9+
* <p>
10+
* Worst-case performance O(log n) Best-case performance O(1) Average
11+
* performance O(log n) Worst-case space complexity O(1)
12+
*
13+
* @author D Sunil (https://github.com/sunilnitdgp)
14+
* @see SearchAlgorithm
15+
*/
816

9-
while (low <= high) {
10-
int mid = (low + high) / 2;
17+
public class PerfectBinarySearch<T> implements SearchAlgorithm {
1118

12-
if (arr[mid] == target) {
13-
return mid;
14-
} else if (arr[mid] > target) {
15-
high = mid - 1;
19+
/**
20+
* @param array is an array where the element should be found
21+
* @param key is an element which should be found
22+
* @param <T> is any comparable type
23+
* @return index of the element
24+
*/
25+
@Override
26+
public <T extends Comparable<T>> int find(T[] array, T key) {
27+
return search(array, key, 0, array.length - 1);
28+
}
29+
30+
/**
31+
* This method implements the Generic Binary Search iteratively.
32+
*
33+
* @param array The array to make the binary search
34+
* @param key The number you are looking for
35+
* @return the location of the key, or -1 if not found
36+
*/
37+
private static <T extends Comparable<T>> int search(T[] array, T key, int left, int right) {
38+
while (left <= right) {
39+
int median = (left + right) >>> 1;
40+
int comp = key.compareTo(array[median]);
41+
42+
if (comp == 0) {
43+
return median; // Key found
44+
}
45+
46+
if (comp < 0) {
47+
right = median - 1; // Adjust the right bound
1648
} else {
17-
low = mid + 1;
49+
left = median + 1; // Adjust the left bound
1850
}
1951
}
20-
return -1;
21-
}
22-
23-
public static void main(String[] args) {
24-
int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
25-
assert PerfectBinarySearch.binarySearch(array, -1) == -1;
26-
assert PerfectBinarySearch.binarySearch(array, 11) == -1;
52+
return -1; // Key not found
2753
}
2854
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import static org.junit.jupiter.api.Assertions.*;
2+
3+
import com.thealgorithms.searches.PerfectBinarySearch;
4+
import org.junit.jupiter.api.Test;
5+
6+
/**
7+
* @author D Sunil (https://github.com/sunilnitdgp)
8+
* @see PerfectBinarySearch
9+
*/
10+
public class PerfectBinarySearchTest {
11+
12+
@Test
13+
public void testIntegerBinarySearch() {
14+
Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
15+
PerfectBinarySearch<Integer> binarySearch = new PerfectBinarySearch<>();
16+
17+
// Test cases for elements present in the array
18+
assertEquals(0, binarySearch.find(array, 1)); // First element
19+
assertEquals(4, binarySearch.find(array, 5)); // Middle element
20+
assertEquals(9, binarySearch.find(array, 10)); // Last element
21+
assertEquals(6, binarySearch.find(array, 7)); // Element in the middle
22+
23+
// Test cases for elements not in the array
24+
assertEquals(-1, binarySearch.find(array, 0)); // Element before the array
25+
assertEquals(-1, binarySearch.find(array, 11)); // Element after the array
26+
assertEquals(-1, binarySearch.find(array, 100)); // Element not in the array
27+
}
28+
29+
@Test
30+
public void testStringBinarySearch() {
31+
String[] array = {"apple", "banana", "cherry", "date", "fig"};
32+
PerfectBinarySearch<String> binarySearch = new PerfectBinarySearch<>();
33+
34+
// Test cases for elements not in the array
35+
assertEquals(-1, binarySearch.find(array, "apricot")); // Element not in the array
36+
assertEquals(-1, binarySearch.find(array, "bananaa")); // Element not in the array
37+
38+
// Test cases for elements present in the array
39+
assertEquals(0, binarySearch.find(array, "apple")); // First element
40+
assertEquals(2, binarySearch.find(array, "cherry")); // Middle element
41+
assertEquals(4, binarySearch.find(array, "fig")); // Last element
42+
}
43+
}

0 commit comments

Comments
 (0)