Skip to content

Commit c3136e2

Browse files
authored
Update and rename Perfect BinarySearch to PerfectBinarySearch.java
1 parent 2890797 commit c3136e2

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

Searches/Perfect BinarySearch

-21
This file was deleted.

Searches/PerfectBinarySearch.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.*;
2+
3+
class PerfectBinarySearch{
4+
5+
static int binarySearch(int[] arr, int target)
6+
{
7+
int low = 0 ;
8+
int high = arr.length - 1 ;
9+
10+
while(low <= high) {
11+
int mid =(low + high) / 2;
12+
13+
if(arr[mid] == target) {
14+
return mid;
15+
}
16+
else if(arr[mid] > target) {
17+
high = mid - 1;
18+
}
19+
else {
20+
low = mid + 1;
21+
}
22+
23+
}
24+
return -1;
25+
}
26+
27+
public static void main(String[] args)
28+
{
29+
PerfectBinarySearch BinarySearch = new PerfectBinarySearch();
30+
int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
31+
assert BinarySearch.binarySearch(array, -1) == -1;
32+
assert BinarySearch.binarySearch(array, 11) == -1;
33+
}
34+
}

0 commit comments

Comments
 (0)