forked from 790hanu/Annex-qr-code-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearch
38 lines (30 loc) · 864 Bytes
/
BinarySearch
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
package Sorting;
public class binarysearch {
public int binarmethod(int a[],int l,int h,int key) {
int mid = (l + h) / 2;
if (a[mid] == key) {
return mid;
}
else if (a[mid] > key) {
return binarmethod(a, l, mid + 1, key);
}
else if (a[mid] < key) {
return binarmethod(a, mid + 1, h, key);
}
return 0;
}
public static void main(String args[])
{
int a[]={10,20,30,40,50,60,70};
int n=a.length;
binarysearch data=new binarysearch();
int low=0;
int high=n;
int key=30;
long e=System.nanoTime();
int ans=data.binarmethod(a,low,high,key);
System.out.println(ans);
long s=System.nanoTime();
System.out.println("to take the total time is the "+(s-e));
}
}