Untitled

 avatar
user_9492606
plain_text
a year ago
804 B
5
Indexable
import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef
{
    public static int binarySearch(int numbers[],int key){
        

        int start=0,end=numbers.length-1,mid=0;
        
        while(start<=end){
            mid=(start+end)/2;
            if(numbers[mid]==key){
                return mid;
            }
            if(numbers[mid]<key){//right -2nd half
                start=mid+1;
            }else{//left -first half
                end=mid-1;
            }
        }
        
        return -1;
    }
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
        int numbers[]={99,34,43,45,56,98,90,42,89};
        int key=-45;
        
        System.out.println("The index of element is "+binarySearch(numbers,key));
	}
}
Editor is loading...
Leave a Comment