- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
Latest commit
26 lines (23 loc) · 688 Bytes
/
Copy pathBinarySearch.java
File metadata and controls
26 lines (23 loc) · 688 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
packagecom.sarvesh.javabasics;
publicclassBinarySearch {
publicstaticvoidmain(String[] args) {
int[] database = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};
inttarget = 23;
System.out.println(search(database, target));
}
publicstaticintsearch(int[] nums, inttarget) {
intleft = 0;
intright = nums.length-1;
while(left<=right){
intmid = left+(right-left)/2;
if(nums[mid] == target){
returnmid;
}elseif(target<nums[mid]){
right = mid-1;
}else{
left = mid+1;
}
}
return -1;
}
}