forked from lemonbashar/java-algo-expert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
52 lines (42 loc) · 1.41 KB
/
BinarySearch.java
File metadata and controls
52 lines (42 loc) · 1.41 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
package algoexpert.easy;
/*
PROBLEM:
Implement Binary Search
Solutions:
1. Recursive - t: O(log n) | s: O(log n)
2. Iterative - t: O(log n) | s: O(1)
*/
public class BinarySearch {
public static void test()
{
int [] testArr = new int[] {0, 1, 21, 33, 45, 45, 61, 71, 72, 73};
int target = 33;
System.out.println(binarySearch(testArr, target));
}
// time : O(log n) | space: O(1)
public static int iterative(int[] array, int target){
int first = 0;
int last = array.length - 1;
while(first <= last)
{
int middle = (first + last) / 2;
if (array[middle] == target) { return middle; }
else if (array[middle] < target) { first = middle + 1;}
else { last = middle - 1;}
}
return -1;
}
// time : O(log n) | space : O(log n) - recursive stack
public static int recursive(int[] array, int target, int first, int last){
if (first > last) {return -1;}
int middle = (first + last) / 2;
if (array[middle] == target) { return middle; }
else if (array[middle] < target) { first = middle + 1;}
else { last = middle - 1; }
return recursive(array, target, first, last);
}
public static int binarySearch(int[] array, int target) {
//return recursive(array, target, 0, array.length - 1);
return iterative(array, target);
}
}