-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.java
More file actions
35 lines (32 loc) · 820 Bytes
/
binarySearch.java
File metadata and controls
35 lines (32 loc) · 820 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
package array;
import java.util.*;
class Binary{
int binSearch(int[] a,int low,int high,int num){
int mid =(low+high)/2;
if(num==a[mid]){
return mid;
}
else if(num>a[mid]){
mid =mid+1;
return binSearch(a,mid,high,num);
}else{
mid=mid-1;
return binSearch(a,low,mid,num);
}
}
}
public class frequency {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the size of array");
int n = in.nextInt();
int[] arr = new int[n];
for(int i=0;i<arr.length;i++){
arr[i]=in.nextInt();
}
System.out.println("Enter the number to srch");
int num =in.nextInt();
Binary obj = new Binary();
System.out.println(obj.binSearch(arr,0,n,num));
}
}