Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
502 B
2
Indexable
Never
public class BinarySearch
{
public static void binarySearch(int arr[],int start,int end,int key)
{
int mid=(start+end)/2;
while(start<=end)
{
if(arr[mid]<key)
{
start=mid+1;
}
else if(arr[mid]==key)
{
System.out.println("element is found at index:"+mid);
break;
}
else
{
end=mid-1;
}
mid=(start+end)/2;
}
if(start>end)
{
System.out.println("lement is not found");
}
}
public static void main(string[]args)
{
int arr[]={10,20,30,40,50);
int key=30;
int end=arr.length-1;
binarySearch(arr,0,end,key);
}
}
Leave a Comment