Thursday, October 23, 2014

Search Insert Position

 binary search

    int searchInsert(int A[], int n, int target) {
       
        int low=0,high=n-1;
        while(low<=high)
        {
            int mid=(low+high)/2;
            if(A[mid]<target)
                low=mid+1;
            else if(A[mid]>target)
                high=mid-1;
            else
                return mid;
        }
        return low;
    }

No comments:

Post a Comment