二分查找(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)]);
}
二分查找(C语言实现)
最新推荐文章于 2023-07-11 19:04:56 发布