-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
38 lines (38 loc) · 805 Bytes
/
Copy pathBinarySearch.java
File metadata and controls
38 lines (38 loc) · 805 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
import java.util.Scanner;
class BinarySearch
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the no. of elements you want:");
int n=sc.nextInt();
int a[]=new int[n];
System.out.println("Enter elements in ascending order");
for(int i=0;i<n;i++)
{
System.out.print("Enter element "+(i+1)+":");
a[i]=sc.nextInt();
}
System.out.print("Enter element you want to search:");
int e=sc.nextInt();
boolean found=false;
int low=0,high=n-1,mid=0;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==e)
{
found=true;
break;
}
else if(e>a[mid])
low=mid+1;
else
high=mid-1;
}
if(found==true)
System.out.println("Found at position "+(mid+1));
else
System.out.println("Not found");
}
}