forked from schugh2001/java-basics-binary-sort
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.java
More file actions
58 lines (55 loc) · 1.18 KB
/
code.java
File metadata and controls
58 lines (55 loc) · 1.18 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.*;
public class sortbinary{
public static int binary(int arr[],int k)
{
int min=minsearch(arr);
int left=-1;
int right=-1;
if(arr[arr.length-1]>=k)
{
left=min;
right=arr.length-1;
}
else{
left=0;
right=min;
}
while(left<=right)
{
int mid=left+(right-left)/2;
if(arr[mid]==k)
{
return mid;
}
else if(arr[mid]<k)
{
left=mid+1;
}
else{
right=mid-1;
}
}
return -1;
}
public static int minsearch(int arr[])
{
int small=Integer.MAX_VALUE,index=-1;
for(int i=0;i<arr.length;i++)
{
if(arr[i]<small)
{
small=arr[i];
index=i;
}
}
// System.out.print(index+" \t");
return index;
}
public static void main(String args[])
{
int arr[]={4,5,6,7,8,0,1,2,3};
int k=3 ;
int index=binary(arr,k);
System.out.print(index);
}
}