折半查找法

来源:互联网 发布:淘宝上cf抽英雄武器 编辑:程序博客网 时间:2024/06/12 18:09

#include <stdio.h>
#include <stdlib.h>

int search(int a[],int l,int r,int g)
{   
    while(l<=r)
    {
        int m=(l+r)/2;   
        if(a[m]==g)return m;  
        if(a[m]>g)r=m-1;
        else l=m+1;
    }
    return -1;
}

main()
{
    int a[50];
   
    for(int x=0;x<50;x++)a[x]=x;
    printf("%d",search(a,10,40,25));
    system("PAUSE");
}

原创粉丝点击