二分查找(C语言实现)

来源:互联网 发布:便民网络测速 编辑:程序博客网 时间:2024/05/22 06:49
二分查找(C语言实现):#include <stdio.h>typedef int ElementType;typedef ElementType* pElement;ElementType Binarysearch(pElement Arr, ElementType x, int Left, int Right){    int Mid = (Right + Left) / 2;    if (x == Arr[Mid])    {        return Mid;    }    else if (Left == Right && x == Arr[Left])    {        return Left;    }    else if (x < Arr[Mid])    {        return Binarysearch(Arr,x,Left,--Mid);    }    else if (x > Arr[Mid])    {        return Binarysearch(Arr, x, ++Mid, Right);    }    else return -1;}void main(void){    ElementType Arr[6] = { 1,2,3,4,5,6};    printf("%d\n",Arr[Binarysearch(Arr,5,0,5)]);}
原创粉丝点击