折半查找(二分查找)

简介:
最基本的算法:
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}
目录
相关文章
|
算法 索引
二分查找(详解)
二分查找(详解)
|
5月前
|
算法 索引
二分查找(一)
二分查找(一)
|
5月前
|
算法 索引
二分查找(二)
二分查找(二)
|
5月前
|
算法 C++
C++021-C++二分查找
C++021-C++二分查找
C++021-C++二分查找
|
5月前
|
存储 算法
03 折半查找
  折半查找又称为二分查找。它仅适用于有序的顺序表。   折半查找的基本思想:首先给定值 key 与表中中间位置的元素比较,若相等,则查找成功,返回该元素的存储位置;若不等,则所需查找的元素只能在中间元素以外的前半部分或后半部分(例如,在查找表升序排列时,若给定值 key 大于中间元素,则所查找的元素只可能在后半部分)。然后在缩小的范围内继续进行同样的查找,如此重复,知道找到位置,或确定表中没有所需要查找的元素,则查找不成功,返回查找失败的信息。  
30 0
|
算法 C语言
这就是二分查找?
本文通过简单的猜数字小游戏向大家介绍二分查找的基本原理。
115 2
|
存储 算法
6-2 二分查找
6-2 二分查找
145 0