-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch_Recursion.java
More file actions
40 lines (39 loc) · 1.18 KB
/
BinarySearch_Recursion.java
File metadata and controls
40 lines (39 loc) · 1.18 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
public class BinarySearch_Recursion {
public static int Binary1(int a[],int left,int right,int key){
if (left<=right){
int mid=(left+right)/2;
if (a[mid]==key){
return mid;
} else if (a[mid]>key) {
return Binary1(a,left,mid-1,key);
}
else if (a[mid]<key){
return Binary1(a,mid+1,right,key);
}
}
return -1;
}
public static void Binary2(int a[],int key){
int left=0;int right=a.length-1;
while (left<=right){
int mid=(left+right)/2;
if (a[mid]==key){
System.out.println("Element found at index "+mid);
break;
}else if(a[mid]>key){
right=mid-1;
}else if (a[mid]<key){
left=mid+1;
}
}
}
public static void main(String[] args) {
int a[]={1,2,3,45,5,7,450};
int x=Binary1(a,0,6,450);
if (x==-1){
System.out.println("Elemet not found...!");
}else{
System.out.println("Elemet found at index "+x);
}
}
}