-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary.java
82 lines (77 loc) · 2.28 KB
/
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
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.io.*;
public class Binary
{
int a[];
int n;
int l;
int u;
Binary(int nn){
n=nn;
l=0;
u=n-1;
a=new int [n];
}
void readData () throws IOException{
BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
System.out.println("Enter the array elements");
for (int i=0;i<n;i++){
a[i]=Integer.parseInt(br.readLine());
}
for (int i=0;i<n;i++){
for (int j=0;j<n-1;j++){
if (a[i]<a[j]){
int tmp=a[i];
a[i]=a[j];
a[j]=tmp;
}
}
}
System.out.println("The sorted array is");
for (int i=0;i<n;i++){
System.out.println(a[i]);
}
}
int binary_search(int v){
int pos;
pos=(l+u)/2;
if (l>u){
return (-1);
}
else if (v==a[pos]){
return pos;
}
else if (v<a[pos]){
u=pos;
return binary_search(v);
}
else{
l=pos;
return binary_search(v);
}
}
public static void main (String args[]) throws IOException{
BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
System.out.println("Enter the array size");
int nn=Integer.parseInt(br.readLine());
Binary ob=new Binary(nn);
ob.readData();
System.out.println("Enter element to search");
int v=Integer.parseInt(br.readLine());
int p=ob.binary_search(v);
if (p!=-1){
System.out.println("The position at which the element was found\t:"+(p+1));
}
else{
System.out.println("The element was not found");
}
}
}
/*
👋 Hi, I’m @aarushinair - Aarushi Nair (she/her/ella)
👀 I’m a Computer Science Engineering Student
💞️ I’m looking to collaborate on #java, #python, #R, #applicationdevelopment
🌱 #GirlsWhoCode #WomenInTech #WomenInIT #WomenInSTEM #CyberSecurity #QuantumComputing #BlockChain #AI #ML
📫 How to reach me: https://www.linkedin.com/in/aarushinair/
👩🏫 YouTube Channel - Code with Aarushi : https://www.youtube.com/channel/UCKj5T1ELHCmkGKujkpqtl7Q
🙋 Follow me on Twitter: https://twitter.com/aarushinair_
*/