-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
64 lines (60 loc) · 2.1 KB
/
Copy pathBinarySearch.java
File metadata and controls
64 lines (60 loc) · 2.1 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.ArrayList;
import java.util.List;
public class BinarySearch {
public static void main(String[] args){
/*
* Binary search
* Given an input of the array 1, 3,5,8,11,19,22,23,34,36,93,99
* input : 10
* input : 11
* input : 34
* */
List<Integer> inputList = new ArrayList<Integer>();
inputList.add(1);
inputList.add(3);
inputList.add(5);
inputList.add(8);
inputList.add(11);
inputList.add(19);
inputList.add(22);
inputList.add(23);
inputList.add(34);
inputList.add(36);
inputList.add(50);
inputList.add(93);
inputList.add(99);
Integer input = 5;
Boolean response = binarySearch(inputList, input);
System.out.println("input : "+ input + " Response is :" + response);
input = 8;
response = binarySearch(inputList, input);
System.out.println("input : "+ input + " Response is :" + response);
input = 18;
response = binarySearch(inputList, input);
System.out.println("input : "+ input + " Response is :" + response);
input = 20;
response = binarySearch(inputList, input);
System.out.println("input : "+ input + " Response is :" + response);
input = 34;
response = binarySearch(inputList, input);
System.out.println("input : "+ input + " Response is :" + response);
}
static boolean binarySearch(List<Integer> inputList, int input) {
int low = 0;
int high = inputList.size()-1;
// System.out.println("input : "+ input + " low :" + low + " high :" + high);
// System.out.println(input);
// System.out.println(inputList.toString());
while(low <= high){
int mid = (low+high)/2;
if (inputList.get(mid).equals(input)) {
return true;
} else if (inputList.get(mid) < input) {
low = mid+1;
} else if (inputList.get(mid) > input) {
high = mid-1;
}
}
return false;
}
}