-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinsear.java
More file actions
42 lines (40 loc) · 1.19 KB
/
Copy pathbinsear.java
File metadata and controls
42 lines (40 loc) · 1.19 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
import java.util.Scanner;
public class binsear {
public static void main(String[] args) {
try (Scanner ab = new Scanner(System.in)) {
int[] arr;
System.out.println("limit");
int n=ab.nextInt();
arr=new int[n];
for (int i = 0; i < arr.length; i++) {
System.out.println("element number"+(i+1)+" : ");
arr[i]=ab.nextInt();
}
System.out.println("element to be searched : ");
int m=ab.nextInt();
binsear ob=new binsear();
int ele=ob.binsearch(arr,m,0,arr.length-1);
if(ele== -1){
System.out.println("element to be searched is not found ");
}
else{
System.out.println("element to be searched is found at index"+ele);
}
}
}
int binsearch(int[] ar,int key,int first,int last){
int mid=(first+last/2);
if(first<=last){
if(key==ar[mid]){
return mid;
}
else if(key<ar[mid]){
binsearch(ar, key, first, mid-1);
}
else{
binsearch(ar, key,mid+1, last);
}
}
return -1;
}
}