-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
executable file
·117 lines (100 loc) · 3.35 KB
/
Copy pathBinarySearch.java
File metadata and controls
executable file
·117 lines (100 loc) · 3.35 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS info.picocli:picocli:4.5.0
import java.util.*;
import java.util.concurrent.Callable;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Parameters;
import static java.lang.System.*;
@Command(
name = "BinarySearch",
mixinStandardHelpOptions = true,
version = "BinarySearch 0.1",
description = "Intersting things with Binary Search")
class BinarySearch implements Callable<Integer> {
@Parameters(index = "0", arity = "0..*", description = "Initial set of values to load")
private List<int[]> inputStrings;
public static void main(String... args) {
int exitCode = new CommandLine(new BinarySearch()).execute(args);
exit(exitCode);
}
@Override
public Integer call() throws Exception {
if (inputStrings == null) {
inputStrings = List.of(
new int[]{-18, -12, -4, 0, 2, 3, 4, 15, 16, 18, 22, 45, 89},
new int[]{2,3,5,9,14,16,18}
);
}
inputStrings.forEach( intArr -> {
out.println("Searching " + Arrays.toString(intArr) + " for 16. Found it at index " + binarySearch(intArr,16));
});
inputStrings.forEach( intArr -> {
int ceiling = ceiling(intArr,15);
out.println("Searching for the ceiling of 10 in " + Arrays.toString(intArr) + ". Found it at index " + ceiling + ". Value of " + intArr[ceiling]);
});
inputStrings.forEach( intArr -> {
int floor = floor(intArr,15);
out.println("Searching for the floor of 15 in " + Arrays.toString(intArr) + ". Found it at index " + floor + ". Value of " + intArr[floor]);
});
return 0;
}
// return the index of the greatest number smaller than or equal to the target
int floor(int[] arr, int target) {
// if the target is less than the lowest number in the array return -1
if (target < arr[0]) {
return -1;
}
int start = 0;
int end = arr.length - 1;
while(start <= end) {
// find the middle element
int mid = start + (end - start) / 2;
if (target < arr[mid]) {
end = mid - 1;
} else if (target > arr[mid]) {
start = mid + 1;
} else {
return mid;
}
}
return end;
}
// return the index of the smallest number greater than or equal to the target
int ceiling(int[] arr, int target) {
// if the target is greater than the greatest number in the array return -1
if (target > arr[arr.length - 1]) {
return -1;
}
int start = 0;
int end = arr.length - 1;
while(start <= end) {
// find the middle element
int mid = start + (end - start) / 2;
if (target < arr[mid]) {
end = mid - 1;
} else if (target > arr[mid]) {
start = mid + 1;
} else {
return mid;
}
}
return start;
}
int binarySearch(int[] arr, int target) {
int start = 0;
int end = arr.length - 1;
while(start <= end) {
// find the middle element
int mid = start + (end - start) / 2;
if (target < arr[mid]) {
end = mid - 1;
} else if (target > arr[mid]) {
start = mid + 1;
} else {
return mid;
}
}
return -1;
}
}