-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchAlgo.java
39 lines (37 loc) · 1.16 KB
/
SearchAlgo.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
39
// import java.util.*;
import java.io.*;
import java.util.Arrays;
class Search {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] arr = new int[10];
System.out.println("Elements: ");
for (int i = 0; i < 10; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
System.out.println("Value: ");
int value = Integer.parseInt(br.readLine());
Arrays.sort(arr);
int flag = 0;
int low = 0;
int high = arr.length-1; // n-1
do {
int mid = (low + high) / 2;
if (arr[mid] == value) {
System.out.println("Found at " + mid);
flag = 1;
break;
} else if (arr[mid] < value){
low = mid + 1;
System.out.println("deducted");
}
else if (arr[mid] > value){
high = mid - 1;
System.out.println("Added");
}
} while (low <= high);
if (flag == 0) {
System.out.println("Not found");
}
}
}