-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathbinarySearch.java
More file actions
36 lines (27 loc) · 838 Bytes
/
binarySearch.java
File metadata and controls
36 lines (27 loc) · 838 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
33
34
35
36
package Recursion;
// Binary search using recursion
public class binarySearch {
public static boolean search(int[] arr, int tar, int l, int r){
if(l <= r)
{
int mid = (l+r)/2;
if(arr[mid] == tar){
return true;
} else if(arr[mid] > tar){
return search(arr, tar, l, mid - 1);
} else {
return search(arr, tar, mid + 1, r);
}
}
return false;
}
public static void main(String[] args){
int arr[] = {10, 25, 40, 55, 70, 95};
int tar = 25;
int l = 0;
int r = arr.length-1;
System.out.println(search(arr, tar, l, r));
}
}
// This article is Contributed by Himanshu Jha.
// Connect with me on linkedin https://www.linkedin.com/in/himanshujhaa/