-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
28 lines (28 loc) · 857 Bytes
/
BinarySearch.java
File metadata and controls
28 lines (28 loc) · 857 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
27
28
public class BinarySearch{
public static boolean binarySearch(int[] arr, int target){
int l = arr.length;
int left = 0;
int right = l - 1;
while(left<=right){
int mid = left + (right - left) / 2;
if(arr[mid] == target){
return true;
} else if(arr[mid]<target){
left=mid+1;
}else{
right=mid-1;
}
}
return false;
}
public static void main(String args[]){
int[] arr = {1,2,3,4,5,6,7,8,9,10};
int target = 7;
boolean found = binarySearch(arr, target);
if(found){
System.out.println("Element " + target + " found in the array.");
} else {
System.out.println("Element " + target + " not found in the array.");
}
}
}