-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMBinarySearch.java
More file actions
53 lines (40 loc) · 1.19 KB
/
MBinarySearch.java
File metadata and controls
53 lines (40 loc) · 1.19 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
package algorithms;
public class MBinarySearch {
public static void run() {
System.out.println("\n==== Binary Search ====\n");
int array[] = new int[1000000];
int target = 777777;
for(int i = 0; i < array.length; i++) {
array[i] = i + 1;
}
long startTime, endTime, elapsedTime;
startTime = System.nanoTime();
// Java includes your own binarySearch as method of the Arrays class
// int index = Arrays.binarySearch(array, target);
int index = MyBinarySearch(array, target);
endTime = System.nanoTime();
elapsedTime = endTime - startTime;
System.out.println("Elapsed Time (Binary Search): " + elapsedTime + " ns");
if(index == -1) {
System.out.println(target + " not found");
}else {
System.out.println("Element found at: " + index);
}
}
public static int MyBinarySearch(int[] list, int item) {
int low = 0;
int high = list.length - 1;
while(low <= high) {
int middle = (low + high) / 2;
int guess = list[middle];
if(guess == item) {
return middle;
} else if(guess > item) {
high = middle - 1;
} else {
low = middle + 1;
}
}
return -1;
}
}