forked from sPredictorX1708/Ultimate-Java-Resources
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarysearch
More file actions
32 lines (27 loc) · 532 Bytes
/
binarysearch
File metadata and controls
32 lines (27 loc) · 532 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
public class binarysearch {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr= {10,20,30,40,50};
int data=30;
System.out.println(binary(arr,data));
}
public static int binary(int[] arr,int data) {
int start=0;
int end=arr.length-1;
while(start<=end)
{
int mid=(start+end)/2;
if(arr[mid]==data)
{
return mid;
}
else if(arr[mid]<data){
start=mid+1;
}
else if(arr[mid]>data){
end=mid-1;
}
}
return -1;
}
}