Best case O(logn)..c++..7ms


  • 0
    H
    class Solution {
    public:
        int search(int A[], int n, int target) {
            
            if(n == 0){
                return -1;
            }
            
            return __search(A , 0 , n-1 , target);
            
        }
        
        private:
        
        int __search(int A[] , int start , int end , int target){
            if(start > end)
                return -1;
            int mid = (start+end)/2;
            
            if(target == A[mid])
                return mid;
            else{
                int x = __search(A , start , mid-1 , target);
                if(x < 0){
                    return __search(A , mid+1 , end , target);
                }else{
                    return x;
                }
            }
        }
    };
    

    My algo is searching in both sides , the best case is O(logn) .


  • 0
    X

    This is binary search? I don't think so;


  • 0
    M

    Is this the same as linear search?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.