/*
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;
            }        
        }        
    }
}

That's not very good code there. No comments, variables that are poorly named so when the author wrote this, they and God knows what they intended. Later, only God knows.

While someone may trace it out the lack of comments and a few paragraphs about what it should do should get this a failing grade if submitted to any professor I know.

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.