0% found this document useful (0 votes)
18 views

Experiment No.3: Program On Array

kxg38919

Uploaded by

ashish
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Experiment No.3: Program On Array

kxg38919

Uploaded by

ashish
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Experiment No.

3
Name:- Swapnil Pandurang Shinde
Roll No:- 2136
 Program on Array.
2.Binary Search
Code:-
import java.util.*;
class b_search
{
public static void main(String arg[])
{
int a[]=new int[10];
int n,i,x,end,mid,beg=0,sum=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter how many no.s = ");
n=sc.nextInt();
System.out.print("Enter numbers = ");
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.print("Enter number you want to find in array = ");
x=sc.nextInt();
end=n;

while(beg<end)
{
mid=(beg+end)/2;
if(x==a[mid])
{
System.out.print(a[mid]+" found ");
break;
}
if(x>a[mid])
{
beg=mid+1;
}
if(x<a[mid])
{
end=mid-1;
}
}
if(beg==n)
{
System.out.print(x+" not found");
}
}
}

Output:-

You might also like