-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathBinarySearch.java
More file actions
41 lines (38 loc) · 1.17 KB
/
Copy pathBinarySearch.java
File metadata and controls
41 lines (38 loc) · 1.17 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
package Searching;
/*Binary search, also known as half-interval search, logarithmic search,
or binary chop, is a search algorithm that finds the position of a
target value within a sorted array. Binary search compares the target
value to the middle element of the array.
Worst complexity: O(log n)
Average complexity: O(log n)
Best complexity: O(1)
Space complexity: O(1)
*/
public class BinarySearch {
public static void main(String[] args) {
int arr[] = {10, 18, 19, 23, 30, 89, 100};
int target = 18;
int start = 0;
int end = arr.length - 1;
int index = -1;
while(start <= end) {
//int mid = (end + start) / 2; -> might cause overflow
int mid = start + (end - start) / 2;
if(arr[mid] < target) {
start = mid + 1;
}
else if(arr[mid] > target) {
end = mid - 1;
}
else {
System.out.println("Found");
index = mid;
break;
}
}
if(start > end) {
System.out.println("Not found");
}
System.out.println("Index: " + index);
}
}