/*
l=first index
r=last index
*/
import java.util.Scanner;
public class BinarySearch{
public static void main(String[]args){
int a[]={1,2,3,4,5,6,7,8,9,10};
Scanner sc=new Scanner(System.in);
System.out.print("Enter a data to search :");
int data=sc.nextInt();
int l=0,r=a.length-1;
while(l<r){
int mid=(l+r)/2;
if(data==a[mid]){
System.out.println("data found at "+mid+"th index.");
break;
}else if(data<a[mid]){
r=mid-1;
}else{
l=mid+1;
}
mid=(l+r)/2;
if(data!=a[mid]){
System.out.print("not found");
break;
}
}
}
}
sujoy98 0 Newbie Poster
rproffitt 2,701 https://5calls.org Moderator
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.