二分查找

#define ID_INLINE inline
// 查找小于value的元素中最大的元素的索引
// array是已经按增序排列好的数组
/*
====================
idBinSearch_GreaterEqual

    Finds the last array element which is smaller than the given value.
====================
*/
template< class type >
ID_INLINE int idBinSearch_Less( const type *array, const int arraySize, const type &value ) {
    int len = arraySize;
    int mid = len;
    int offset = 0;
    while( mid > 0 ) {
        mid = len >> 1;
        if ( array[offset+mid] < value ) {
            offset += mid;
        }
        len -= mid;
    }
    return offset;
}

/*
====================
idBinSearch_GreaterEqual

    Finds the last array element which is smaller than or equal to the given value.
====================
*/
template< class type >
ID_INLINE int idBinSearch_LessEqual( const type *array, const int arraySize, const type &value ) {
    int len = arraySize;
    int mid = len;
    int offset = 0;
    while( mid > 0 ) {
        mid = len >> 1;
        if ( array[offset+mid] <= value ) {
            offset += mid;
        }
        len -= mid;
    }
    return offset;
}

/*
====================
idBinSearch_Greater

    Finds the first array element which is greater than the given value.
====================
*/
template< class type >
ID_INLINE int idBinSearch_Greater( const type *array, const int arraySize, const type &value ) {
    int len = arraySize;
    int mid = len;
    int offset = 0;
    int res = 0;
    while( mid > 0 ) {
        mid = len >> 1;
        if ( array[offset+mid] > value ) {
            res = 0;
        } else {
            offset += mid;
            res = 1;
        }
        len -= mid;
    }
    return offset+res;
}

/*
====================
idBinSearch_GreaterEqual

    Finds the first array element which is greater than or equal to the given value.
====================
*/
template< class type >
ID_INLINE int idBinSearch_GreaterEqual( const type *array, const int arraySize, const type &value ) {
    int len = arraySize;
    int mid = len;
    int offset = 0;
    int res = 0;
    while( mid > 0 ) {
        mid = len >> 1;
        if ( array[offset+mid] >= value ) {
            res = 0;
        } else {
            offset += mid;
            res = 1;
        }
        len -= mid;
    }
    return offset+res;
}

你可能感兴趣的:(二分查找)