-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
39 lines (30 loc) · 1.38 KB
/
Copy pathBinarySearch.java
File metadata and controls
39 lines (30 loc) · 1.38 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
public class BinarySearch {
public static void main(String[] args){
//Binary search algorithm is logarithmic in time complexity O(log n) as the data increases the time complexity increases logarithmically,
//It works on the sorted array where it divides the array into two parts and then search for the element in the array.
//it checks if it is equal to the middle value is not then it checks is it is greater or smaller, if greater then it checks the right part of the array and if smaller then it checks the left part of the array.
int[] arr = new int[100];
int target = 1;
for(int i=0; i<arr.length; i++){
arr[i] = i+1;
}
int result = binarySearch(arr, target);
if(result == -1) System.out.println("The element is not present in the array");
else System.out.println("The element is present at index: "+result+" in the array");
}
public static int binarySearch(int[] arr, int target){
int start = 0;
int end = arr.length -1;
while(start <= end){
int mid = start + (end - start) / 2;
if(target < arr[mid]){
end = mid -1;
}else if(target > arr[mid]){
start = mid + 1;
}else {
return mid;
}
}
return -1;
}
}