0% found this document useful (0 votes)
8 views1 page

Java Binary Searching

Uploaded by

fojimaw327
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

Java Binary Searching

Uploaded by

fojimaw327
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

Scanner;
class main
{
int search(int k[],int data,int l,int r)
{
while(l<=r)
{
int mid=(l+r)/2;
if(data==k[mid])
return mid;
else if(data<k[mid])
r=mid-1;
else
l=mid+1;
}
return -1;
}
public static void main(String[] args)
{
int i;
Scanner sc= new Scanner(System.in);
int a[]=new int[10];
System.out.println("Enter the elements");
for(i=0;i<10;i++)
{
int num=sc.nextInt();
a[i]=num;
}
System.out.println("The elements are");
for(i=0;i<10;i++)
{
System.out.println(" "+a[i]);
}
System.out.println("Enter the element to be searched");
int item=sc.nextInt();
main obj=new main();
int element=obj.search(a,item,0,9);
if(element==-1)
{
System.out.println("The element is not found ");
}
else
{
System.out.println("The element is found at "+i);
}
}
}

You might also like