二分搜索

来源:互联网 发布:hbo美剧推荐知乎 编辑:程序博客网 时间:2024/05/29 08:44
#ifndef _BINARY_SEARCH_H_
#define _BINARY_SEARCH_H_
/**
* 从array[beginIndex,endIndex] 中查找value的下标索引。
* array按递增排序,无重复数字。 
* cost是用来返回时间复杂度的。
* 返回value在array中的下标索引。-1表示没有找到。
*/
int binarySearch_asc_nonduplicate(int array[],int beginIndex,int endIndex,int value,int* cost){
    int keyIndex = -1;
    while(beginIndex <= endIndex){
        ++(*cost);
        int midIndex = (endIndex - beginIndex) / 2 + beginIndex;
        int midValue = array[midIndex];
        if(value == midValue){
            keyIndex = midIndex;
            break;
        }
        else if(value < midValue){
            endIndex = midIndex-1;
        }
        else if(value > midValue){
            beginIndex = midIndex+1;
        }
    }


    return keyIndex;
}


/**
* 从array[beginIndex,endIndex] 中查找value的下标索引。
* array按递增排序,有重复数字(返回第一个相同的元素下标)。也支持无重复数字。 
* cost是用来返回时间复杂度的。
* 返回value在array中的下标索引。-1表示没有找到。
*/
int binarySearch_first_asc_duplicate(int array[],int beginIndex,int endIndex,int value,int* cost){
    int keyIndex = -1;
    while(beginIndex <= endIndex){
        ++(*cost);
        int midIndex = (endIndex - beginIndex) / 2 + beginIndex;
        int midValue = array[midIndex];
        if(value == midValue){
            keyIndex = midIndex;
            endIndex = midIndex-1;
        }
        else if(value < midValue){
            endIndex = midIndex-1;
        }
        else if(value > midValue){
            beginIndex = midIndex+1;
        }
    }


    return keyIndex;
}


/**
* 从array[beginIndex,endIndex] 中查找value的下标索引。
* array按递增排序,有重复数字(返回最后一个相同的元素下标)。也支持无重复数字。 
* cost是用来返回时间复杂度的。
* 返回value在array中的下标索引。-1表示没有找到。
*/
int binarySearch_last_asc_duplicate(int array[],int beginIndex,int endIndex,int value,int* cost){
    int keyIndex = -1;
    while(beginIndex <= endIndex){
        ++(*cost);
        int midIndex = (endIndex - beginIndex) / 2 + beginIndex;
        int midValue = array[midIndex];
        if(value == midValue){
            keyIndex = midIndex;
            beginIndex = midIndex+1;
        }
        else if(value < midValue){
            endIndex = midIndex-1;
        }
        else if(value > midValue){
            beginIndex = midIndex+1;
        }
    }


    return keyIndex;
}


/**
* 从array[beginIndex,endIndex] 中查找value的下标索引。
* array按递减排序,无重复数字。 
* cost是用来返回时间复杂度的。
* 返回value在array中的下标索引。-1表示没有找到。
*/
int binarySearch_desc_nonduplicate(int array[],int beginIndex,int endIndex,int value,int* cost){
    int keyIndex = -1;
    while(beginIndex <= endIndex){
        ++(*cost);
        int midIndex = (endIndex - beginIndex) / 2 + beginIndex;
        int midValue = array[midIndex];
        if(value == midValue){
            keyIndex = midIndex;
            break;
        }
        else if(value < midValue){
            beginIndex = midIndex+1;
        }
        else if(value > midValue){
            endIndex = midIndex-1;
        }
    }


    return keyIndex;
}




/**
* 从array[beginIndex,endIndex] 中查找value的下标索引。
* array按递减排序,有重复数字(返回第一个相同的元素下标)。也支持无重复数字。 
* cost是用来返回时间复杂度的。
* 返回value在array中的下标索引。-1表示没有找到。
*/
int binarySearch_first_desc_duplicate(int array[],int beginIndex,int endIndex,int value,int* cost){
    int keyIndex = -1;
    while(beginIndex <= endIndex){
        ++(*cost);
        int midIndex = (endIndex - beginIndex) / 2 + beginIndex;
        int midValue = array[midIndex];
        if(value == midValue){
            keyIndex = midIndex;
            endIndex = midIndex-1;
        }
        else if(value < midValue){
            beginIndex = midIndex+1;
        }
        else if(value > midValue){
            endIndex = midIndex-1;
        }
    }


    return keyIndex;
}


/**
* 从array[beginIndex,endIndex] 中查找value的下标索引。
* array按递减排序,有重复数字(返回最后一个相同的元素下标)。也支持无重复数字。 
* cost是用来返回时间复杂度的。
* 返回value在array中的下标索引。-1表示没有找到。
*/
int binarySearch_last_desc_duplicate(int array[],int beginIndex,int endIndex,int value,int* cost){
    int keyIndex = -1;
    while(beginIndex <= endIndex){
        ++(*cost);
        int midIndex = (endIndex - beginIndex) / 2 + beginIndex;
        int midValue = array[midIndex];
        if(value == midValue){
            keyIndex = midIndex;
            beginIndex = midIndex+1;
        }
        else if(value < midValue){
            beginIndex = midIndex+1;
        }
        else if(value > midValue){
            endIndex = midIndex-1;
        }
    }


    return keyIndex;
}




#endif
0 0
原创粉丝点击