-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path13_binary.java
69 lines (63 loc) · 1.17 KB
/
13_binary.java
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
59
60
61
62
63
64
65
66
67
68
69
import java.io.*;
class binary
{
int a[]=new int[10];
int n,i,j,c,se,start,end,mid,min;
void input()throws Exception
{
DataInputStream d=new DataInputStream(System.in);
System.out.print("Enter the no of elements:");
n=Integer.parseInt(d.readLine());
System.out.print("Enter the elements");
for(i=0;i<n;i++)
a[i]=Integer.parseInt(d.readLine());
System.out.print("Enter the elements to be searched:");
se=Integer.parseInt(d.readLine());
}
void sort()
{
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[min]>a[j])
min=j;
}
if(min!=i)
{
c=a[min];
a[min]=a[i];
a[i]=c;
}
}
}
void print()
{
start=0;end=n-1;
for(;start<=end;)
{
mid=(start+end)/2;
if(a[mid]==se)
break;
else if(a[mid]>se)
end=mid-1;
else
start=mid+1;
}
if(start<=end)
System.out.println("Element found");
else
System.out.println("Element not found");
}
}
class main
{
public static void main(String a[])throws Exception
{
binary p=new binary();
p.input();
p.sort();
p.print();
}
}