-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
46 lines (39 loc) · 902 Bytes
/
Copy pathBinarySearch.java
File metadata and controls
46 lines (39 loc) · 902 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
36
37
38
39
40
41
42
43
44
45
46
package array;
import java.util.Arrays;
public class BinarySearch {
public static void main(String[] args) {
int a[]= {10,40,56,34,90,45,34,23};
int res=binarySearch(sort(a), 45);
System.out.println(res>=0?"element present at "+res+" position":res);
}
public static int[] sort(int[]a) {
for(int i=0;i<a.length;i++) {
for(int j=0;j<a.length-1;j++)
if(a[j]>a[j+1]) {
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
System.out.println(Arrays.toString(a));
return a;
}
public static int binarySearch(int[]a,int ele) {
int first=0,last=a.length-1;
int mid=(first+last)/2;
while(first<last) {
if(a[mid]==ele) {
return mid;
}
else if(a[mid]<ele) {
first=mid+1;
}
else if(a[mid]>ele) {
last=mid-1;
}
System.out.println(mid);
mid=(first+last)/2;
}
return -1;
}
}