forked from Swatigupta-droid/Basic-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
34 lines (27 loc) · 876 Bytes
/
BinarySearch.java
File metadata and controls
34 lines (27 loc) · 876 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
29
30
31
32
33
34
public class BinarySearch{
public static boolean findNumber(int arr[], int target){
int high = arr.length;
int low = 0;
while(low <= high){
int mid = low + (high-low) / 2;
if(arr[mid] == target){
return true;
}
else if(arr[mid] < target){
low = mid + 1;
}
else if(arr[mid] > target){
high = miw - 1;
}
}
return false;
}
public static void main(String args[]){
//Array should always be in sorted order for binary search
int arr[] = { 1, 3, 5, 6, 8, 9 };
System.out.println("Enter the number to be searched");
Scanner sc = new Scanner(System.in);
int target = sc.nextInt();
System.out.println(findNumber(arr, target));
}
}