-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
91 lines (74 loc) · 2.59 KB
/
Copy pathBinarySearch.java
File metadata and controls
91 lines (74 loc) · 2.59 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
package chapterTwo;
/**
* Recursive and iterative implementations of a binary search array.
* @author Jeff
* @since 2018-02-18
*/
public class BinarySearch {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Test case 1
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
//Both should return -1 (the int is not in the array)
System.out.println(iterativeBinarySearch(numbers, 11));
System.out.println(recursiveBinarySearch(numbers, 0, numbers.length-1, 11));
//Test case 2
int[] numbers2 = new int[100];
for(int i = 0; i < 100; i++) {
numbers2[i] = i;
}
//Both should return 56 (index is the same is the number in the array)
System.out.println(iterativeBinarySearch(numbers2, 56));
System.out.println(recursiveBinarySearch(numbers2, 0, numbers2.length-1, 56));
}
/**
* Recursive implementation of a binary search.
* Return a -1 if the searchFor number is not found, otherwise return the
* index it was found at
*
* @param numbers array to be searched
* @param start starting index of array
* @param end final index of array
* @param searchFor int to be searched for
* @return int index where the number was found (or -1)
*/
public static int recursiveBinarySearch(int[] numbers, int start, int end, int searchFor) {
int midPoint = start + ((end - start) / 2);
if (numbers[midPoint] == searchFor)
return midPoint;
else if (start >= end)
return -1;
else if (numbers[midPoint] > searchFor)
return recursiveBinarySearch(numbers, 0, midPoint - 1, searchFor);
else if (numbers[midPoint] < searchFor)
return recursiveBinarySearch(numbers, midPoint + 1, end, searchFor);
return -1;
}
/**
* Iterative implementation of a binary search.
* Return a -1 if the searchFor number is not found, otherwise return the
* index it was found at
*
* @param numbers array to be searched
* @param searchFor int to be searched for
* @return int index where the number was found (or -1)
*/
public static int iterativeBinarySearch(int[] numbers, int searchFor) {
int midpoint = (numbers.length-1) / 2;
int startpoint = 0;
int endpoint = numbers.length -1;
while (startpoint <= endpoint) {
if (numbers[midpoint] == searchFor) //Number found
return midpoint;
else if (numbers[midpoint] > searchFor) {
startpoint = 0;
endpoint = midpoint - 1;
}
else
startpoint = midpoint + 1;
//Point to the new midpoint of the search array (halfway btwn start and end)
midpoint = startpoint + ((endpoint - startpoint) / 2);
}
return -1;
}
}