-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchRecursion.java
More file actions
50 lines (38 loc) · 1.34 KB
/
BinarySearchRecursion.java
File metadata and controls
50 lines (38 loc) · 1.34 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
42
43
44
45
46
47
48
49
50
import java.util.*;
public class BinarySearchRecursion {
int binarysearch(int[] arr,int l,int r, int x){
if(r>=l){
int m= l+ (r-l)/2;
if(arr[m]==x){
return m;
}
else if(arr[m]<x){
return binarysearch(arr,m+1,r,x);
}
else{
return binarysearch(arr,l,m-1,x);
}
}
return -1;
}
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number of elements inside your array :-");
int n= sc.nextInt();
int[] arr= new int[n];
System.out.println("Enter the values for each and every index(GIVE ELEMENTS IN ASCENDING ORDER)");
for(int i=0;i<n;i++){
arr[i]= sc.nextInt();
}
System.out.println("What number are you finding?");
int x= sc.nextInt();
BinarySearchRecursion obj= new BinarySearchRecursion();
int result= obj.binarysearch(arr,0,n-1,x);
if(result==-1){
System.out.println("We were not able to find the number you were looking for inside the array you provided us!");
}
else{
System.out.println("We were able to find your value which was :- "+x+" at index value :- "+ result);
}
}
}