-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearch.java
75 lines (63 loc) · 2.81 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
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
65
66
67
68
69
70
71
72
73
74
75
class BinarySearch {
public static void main(String args[]) {
System.out.println("\nTest with Odd Number of Elements");
int[] test0 = new int[]{1, 4, 9};
System.out.println(BinarySearch(test0, 4));
System.out.println(BinarySearch(test0, 9));
System.out.println(BinarySearch(test0, 1));
System.out.println(BinarySearch(test0, 0));
System.out.println("\nTest with all Duplicates");
int[] test1 = new int[]{1, 1, 1, 1};
System.out.println(BinarySearch(test1, 1));
System.out.println(BinarySearch(test1, 0));
System.out.println("\nTest with One Value");
int[] test2 = new int[]{-1};
System.out.println(BinarySearch(test2, -1));
System.out.println(BinarySearch(test2, 0));
System.out.println("\nTest with Even Length Array with Large Individual Values");
int[] test3 = new int[]{0, 2, 214783647, 214783647};
System.out.println(BinarySearch(test3, 2));
System.out.println(BinarySearch(test3, 214783647));
System.out.println("\nTest With Empty Array");
int[] test4 = new int[]{};
System.out.println(BinarySearch(test4, 100));
System.out.println("\nTest with Unsorted Array, kept in a try catch to handle any unidentified behaviour. "
+ " Coincidentally, returns Value if value checked is the first Mid Value.");
try {
int[] test5 = new int[]{13, 4, 11, 9, 0, 0, -1};
System.out.println(BinarySearch(test5, 14));
System.out.println(BinarySearch(test5, 9));
} catch(Exception e) {
System.out.println(e.getLocalizedMessage());
}
System.out.println("\nTest with a Huge Array and Huge Values");
int[] test6 = new int[42956731];
int count = 0;
for(int i = -21478365; count < 42956731; i++) {
test6[count] = i;
count++;
}
System.out.println(BinarySearch(test6, 214783647));
System.out.println(BinarySearch(test6, 21478365));
System.out.println(BinarySearch(test6, -21478365));
System.out.println(BinarySearch(test6, 4000));
}
private static String BinarySearch(int[] input, int valueToSearch) {
int left = 0;
int right = input.length - 1;
int mid = (left + right) >> 1; // Used to handle Overflow Error.
while((left <= right) && input[mid] != valueToSearch) {
if(input[mid] > valueToSearch) {
right = mid - 1;
} else if(input[mid] < valueToSearch) {
left = mid + 1;
}
mid = (left + right) / 2;
}
if(left > right) {
return "Value " + valueToSearch +" Not Found";
} else {
return ("Number " + valueToSearch + " found at Array Position " + mid);
}
}
}