[演算法]各種二分查詢

orchid發表於2014-10-20

1,給定一個有序陣列values,求任意一個i使得values[i]等於v,不存在返回-1

int search(int* values,int len,int key)
{
    if(!values || len <=0) return -1;
    int low=0;
    int high=len-1;
    while(low<=high)
    {
        int mid =(low+high)/2;
        if(values[mid]==key) return mid;
        else if(values[mid]>key) high=mid-1;
        else low = mid +1;
    }
    return -1;
}

 

2,給定一個有序陣列values,求最小的i使得values[i]等於v,不存在返回-1

int searchFirst(int * values,int len,int key)
{
    if(!values || len <=0) return -1;
    int low=0;
    int high=len-1;
    while(low<=high)
    {
        int mid =(low+high)/2;
        if(values[mid]==key)
        {
            if(mid==0) return mid;
            else if(values[mid-1]<key) return mid;
            else high=mid-1;
        }
        else if (values[mid]>key) high=mid-1;
        else low=mid+1;
    }
    return -1;
}

 

3,給定一個有序陣列values,求最大的i使得values[i]等於v,不存在返回-1

int searchLast(int* values,int len,int key)
{
    if(!values || len <=0) return -1;
        int low=0;
        int high=len-1;
        while(low<=high)
        {
            int mid =(low+high)/2;
            if(values[mid]==key)
            {
                if(mid==len-1) return mid;
                else if(values[mid+1]>key) return mid;
                else low=mid+1;
            }
            else if (values[mid]>key) high=mid-1;
            else low=mid+1;
        }
    return -1;
}

 

4,給定一個有序陣列values,求最大的i使得values[i]小於v,不存在返回-1

int searchLargestSmallThan(int* values,int len,int key)
{
    if(!values || len <=0) return -1;

    int low=0,high=len-1;
    while(low <= high){
        int mid = (low+high)/2;
        if(values[mid]>=key) high=mid-1;
        else{
            if(mid<len-1 && values[mid+1]>=key) return mid;
            else
                low=mid+1;
        }
    }
    return -1;
}

 

4,給定一個有序陣列values,求最小的i使得values[i]大於v,不存在返回-1

int searchSmallestLargeThan(int* values, int len,int key)
{
    if(!values || len <=0) return -1;
    int low=0,high=len-1;
    while(low <= high){
        int mid = (low+high)/2;
        if(values[mid]<=key) low=mid+1;
        else{
            if(mid>0 && values[mid-1]<=key) return mid;
            else
                high=mid-1;
        }
    }
    return -1;
}

 

又添新成員...

5,陣列A絕對值有序,絕對值相同的負值出現在正值前面,返回key出現的第一個位置

比如陣列 -1 -1 1 1 1 -2 -2 -2 2 2 2 2 2 -3 4 -5 6 -7 7 7 7 7 8  key=2時返回8

int searchInAbsOrder(int* values,int len,int key)
{
    int i=0,j=len-1;
    while(i<=j)
    {
        int mid=(i+j)/2;
        if(values[mid]==key)
        {
            if(mid==0) return mid;
            else if(values[mid]>values[mid-1])
            {
                return mid;
            }
            else
            {
                j=mid-1;
            }
        }
        else if(values[mid]==-key)
        {
            if(key>=0)
            {
                i=mid+1;
            }
            else
            {
                j=mid-1;
            }
        }
        else if(abs(values[mid])>abs(key))
        {
            j=mid-1;
        }
        else
        {
            i=mid+1;
        }
    }
    return -1;
}

 

相關文章