-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchNew.java
More file actions
33 lines (27 loc) · 885 Bytes
/
Copy pathBinarySearchNew.java
File metadata and controls
33 lines (27 loc) · 885 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
import java.util.*;
public class BinarySearchNew {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
int left = 0;
int right = arr.length - 1;
System.out.println("Enter Number To Search:");
int num = sc.nextInt();
boolean isFound = false;
while (left <= right) {
int mid = (left + right) / 2;
if (arr[mid] == num) {
isFound = true;
System.out.println("Number Found = " + arr[mid]);
break;
} else if (arr[mid] > num) {
right = mid - 1;
} else if (arr[mid] < num) {
left = mid + 1;
}
}
if (!isFound) {
System.out.println("Number Not Found");
}
}
}