折半查找(二分查找)

最基本的算法:
None.gif int BinarySearch( int arr[],  int len,  int searchKey)
ExpandedBlockStart.gif {
InBlock.gif    int start = 0, end = len - 1, mid;
InBlock.gif    while (start <= end)
ExpandedSubBlockStart.gif    {
InBlock.gif        // 求中间的索引值
InBlock.gif
        mid = (start + end) / 2;
InBlock.gif
InBlock.gif        if (arr[mid] == number)
ExpandedSubBlockStart.gif        {
InBlock.gif            // 查到了
InBlock.gif
            return mid;
ExpandedSubBlockEnd.gif        }
InBlock.gif        else if (arr[mid] < searchKey)
ExpandedSubBlockStart.gif        {
InBlock.gif            // 搜索范围缩小到后半部
InBlock.gif
            start = mid + 1;
ExpandedSubBlockEnd.gif        }
InBlock.gif        else if (arr[mid] > searchKey)
ExpandedSubBlockStart.gif        {
InBlock.gif            // 搜索范围缩小到前半部
InBlock.gif
            end = mid - 1;
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
InBlock.gif
InBlock.gif    // 没查到
InBlock.gif
    return -1;
ExpandedBlockEnd.gif}


用到递归的算法:
None.gif int BinarySearch(  const  int arr[],  int searchKey,  int start,  int end )
ExpandedBlockStart.gif {
InBlock.gif    // 求中间的索引值
InBlock.gif
    int mid = ( start + end ) / 2;
InBlock.gif
InBlock.gif    if ( searchKey == arr[ mid ] )
ExpandedSubBlockStart.gif    {
InBlock.gif        // 找到了
InBlock.gif
        return mid;
ExpandedSubBlockEnd.gif    }
InBlock.gif    else if ( searchKey < arr[ mid ] )
ExpandedSubBlockStart.gif    {
InBlock.gif        // 搜索范围缩小到前半部
InBlock.gif
        return BinarySearch( arr, searchKey, start, mid - 1 );
ExpandedSubBlockEnd.gif    }
InBlock.gif    else if ( searchKey > arr[ mid ] )
ExpandedSubBlockStart.gif    {
InBlock.gif        // 搜索范围缩小到后半部
InBlock.gif
        return BinarySearch( arr, searchKey, mid + 1, end );
ExpandedSubBlockEnd.gif    }
InBlock.gif    else
ExpandedSubBlockStart.gif    {
InBlock.gif        // 没有查找到
InBlock.gif
        return -1;
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}


使用迭代实现:
None.gif int BinarySearch(  const  int arr[],  const  int& len,  const  int& searchKey )
ExpandedBlockStart.gif {  
InBlock.gif    int start=0, end=len-1, mid;
InBlock.gif    while ( start < end )
ExpandedSubBlockStart.gif    {
InBlock.gif        mid = (start + end) / 2;
InBlock.gif
InBlock.gif        if ( searchKey > arr[mid] )
ExpandedSubBlockStart.gif        {
InBlock.gif            start = mid + 1;
ExpandedSubBlockEnd.gif        }
InBlock.gif        else
ExpandedSubBlockStart.gif        {
InBlock.gif            end = mid;
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
InBlock.gif
InBlock.gif    if ( start > end )
ExpandedSubBlockStart.gif    {
InBlock.gif        return -1;
ExpandedSubBlockEnd.gif    }
InBlock.gif    else
ExpandedSubBlockStart.gif    {
InBlock.gif        if ( searchKey == arr[start] )
ExpandedSubBlockStart.gif        {
InBlock.gif            return start;
ExpandedSubBlockEnd.gif        }
InBlock.gif        else
ExpandedSubBlockStart.gif        {
InBlock.gif            return -1;
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}



测试代码:
None.gif void testBinarySearch()
ExpandedBlockStart.gif {
InBlock.gif    const int LEN = 8;
ExpandedSubBlockStart.gif    int a[LEN] = { 1, 2, 2, 2, 5, 6, 8, 9 };
InBlock.gif
InBlock.gif    printf("查找到的索引号为:%d\n", BinarySearch(a, LEN, 5));
InBlock.gif
InBlock.gif    printf("查找到的索引号为:%d\n", BinarySearch(a, 5, 0, LEN-1));
ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值