二分查找法

用于有序序列

#include <iostream>
using namespace std;

int erfen(int a[],int r,int target)
{
    int low=0,hight=r,middle;
    while(low<=hight)
    {
        middle=(low+hight)/2;
        if(a[middle]==target)
        {
            return middle;
        }
        else if(target>a[middle])
        {
            low=middle+1;
        }
        else if(target<a[middle])
        {
            height=middle-1;
        }
    }
    return -1;
}

int main ()
{
    int a[10]={2,3,5,8,9,10,12,13,15,23};
    int index=erfen(a,9,23);
    cout << index <<endl;
    return 0;
}

上一篇:打开文件所在的文件夹并选中该文件的C++代码源码


下一篇:Aliyun Serverless VSCode Extension v1.17.0 发布