forked from I-RoshanKumar/Beginner_Hactoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearch.java
38 lines (27 loc) · 981 Bytes
/
BinarySearch.java
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
public class BinarySearch {
public static void main(String[] args) {
int arr[] = {1, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59};
int upper = arr.length;
int lower = 0;
int x = 23;
int result = binarySearch(arr, lower, upper - 1, x);
if (result == -1)
System.out.println("Couldn't find.");
else
System.out.println("Element found at index " + result);
}
static int binarySearch(int arr[], int low, int up, int x)
{
if (up >= low) {
int mid = (low + up) / 2;
if (x == arr[mid])
return mid;
else if (x < arr[mid])
return binarySearch(arr, low, mid - 1, x);
else{
return binarySearch(arr, mid + 1, up, x);
}
}
return -1;
}
}