Preview:
class binarySearch
{
    public static void main(String[] arg)
    {
        int n,key;
        n=7;
        int start=0;
        int end=n-1;
        
        key=8;
        int arr[]={2,3,4,7,8,9};
        while(start<=end)
        {
            int mid=start+ (end-start)/2;
            if(arr[mid]==key)
            {
                System.out.println("Element is "+ key +" which present at " +mid);
                break;
            }
            if(arr[mid]<key)
            {
                start=mid+1;
            }
            else{
                end=mid-1;
            }
            if(start>end)
            {
                System.out.println("not present in array");
            }
        }
        
    }
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter