-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBinarySearch.java
More file actions
25 lines (24 loc) · 826 Bytes
/
BinarySearch.java
File metadata and controls
25 lines (24 loc) · 826 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
import java.util.Scanner;
public class BinarySearch {
public static void main(String[] args) {
int[] arr = { 2, 5, 8, 10, 13, 15, 18, 20 };
int target, left = 0, right = arr.length - 1, mid;
Scanner input = new Scanner(System.in);
System.out.println("Enter the element to search: ");
target = input.nextInt();
while (left <= right) {
mid = (left + right) / 2;
if (arr[mid] == target) {
System.out.println(target + " found at index " + mid);
break;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
if (left > right) {
System.out.println(target + " not found in the array.");
}
}
}