-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearch.java
38 lines (36 loc) · 1.09 KB
/
BinarySearch.java
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
35
36
37
38
import java.util.Scanner;
public class BinarySearch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int arr[] = { 1, 2, 4, 6, 11, 25, 34, 67, 69, 84 };
System.out.println("Enter the number to be searched");
int key = sc.nextInt();
sc.close();
int indx = binarySearch(arr, key);
if (indx != -1) {
System.out.println("Element found at index " + indx);
} else {
System.out.println("Element not found");
}
}
static int binarySearch(int[] arr, int key) {
int low = 0, highest = arr.length - 1, indx = 0;
boolean flag = false;
while (low <= highest) {
int mid = (low + highest) / 2;
if (arr[mid] < key) {
low = mid + 1;
} else if (arr[mid] > key) {
highest = mid - 1;
} else{
flag = true;
indx = mid;
break;
}
if (!flag) {
indx = -1;
}
}
return indx;
}
}