-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearching.java
More file actions
32 lines (30 loc) · 793 Bytes
/
Copy pathSearching.java
File metadata and controls
32 lines (30 loc) · 793 Bytes
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
public class Searching {
public static void main(String[] args) {
Searching search = new Searching();
int [] arr = {1,2,4,5,7,8,10,23};
search.BinarySearch(2, arr);
}
// binary search
// splits the array in half cutting the time compleixty
/*
* best case: O(1) // get it first try
* average case: O(logn)
* worst case: O(logn)
*/
public int BinarySearch(int findelement,int [] arr) {
int index = 0;
int first = 0;
int last = arr.length - 1;
int midpoint =(first+last)/2;
while(arr[midpoint]!=findelement) {
if(findelement<arr[midpoint]) {
last = midpoint;
} else if(findelement>arr[midpoint]) {
first = midpoint;
} else {
return -1;
}
}
return -1;
}
}