Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Java by registered user im-rakesh0827 ( 4 years ago )
public class BinarySearchRecursion {

    static int search(int arr[], int target, int start, int end){

        int ans=0;
        if(start>end){
            return -1;
        }else{
            int mid = (start+end)/2;
            if(arr[mid]==target){
                ans= mid;
            }else if(target<arr[mid]){
                end = mid-1;
                ans = search(arr, target, start, end);
            }else{
                start = mid+1;
                ans =  search(arr, target, start, end);
            }

        }
        return ans; 

    }
    public static void main(String[] args) {

        int A[] = {1, 4, 5, 77, 87, 89, 90};
        int s = 0;
        int e = A.length-2;
        int key = 80;
        
        System.out.println(search(A, key, s, e));
    }
    
}

 

Revise this Paste

Your Name: Code Language: