二分法查找可以用循环和递归两种方式实现
#include <stdio.h>
typedef int DataType;
typedef unsigned int uint32;
typedef int int32;
#define SEARCH_LIST_SIZE (8)
int32 BinarySearch(DataType* plist, uint32 len, DataType item); //循环方式
int32 BinarySearchRecur(DataType* plist, uint32 len, DataType item); //递归方式
int main(int argc, char *const argv[])
{
int32 ret = 0;
DataType list[SEARCH_LIST_SIZE] = {1,2,3,4,5,6,7,8};
if((ret = BinarySearchRecur(list, SEARCH_LIST_SIZE, 2)) != -1)
{
printf("Item is found, %d
", ret);
}
else
{
printf("Do not find
");
}
}
/*----------------------------------------------------------------------
* Function: int BinarySearch(DataType * plist, uint32 len, DataType item)
* Discription: 二分查找(循环方式)
* Inputs: plist, 指向被查找有序序列的指针;
* len, 被查找序列长度;
* item, 被查找数据
* Outputs: none
* return: -1, 未找到;
* -2, 输入参数异常;
* 其他, 被查找元素对应的数组下标
* ---------------------------------------------------------------------*/
int32 BinarySearch(DataType* plist, uint32 len, DataType item)
{
uint32 icnt = 0;
int32 min = 0, mid = 0, max = len-1; //需定义为有符号类型
if(plist == NULL)
{
return -2;
}
else
{
while(min <= max)
{
mid = (min + max)/2;
if(plist[mid] < item)
{
min = mid + 1;
}
else if(plist[mid] > item)
{
max = mid - 1;
}
else
{
return mid;
}
}
return -1;
}
}
/*----------------------------------------------------------------------
* Function: BinarySearchRecur(DataType* plist, uint32 len, DataType item)
* Discription: 二分查找(递归方式)
* Inputs: plist, 指向被查找有序序列的指针;
* len, 被查找序列长度;
* item, 被查找数据
* Outputs: none
* return: -1, 未找到;
* -2, 输入参数异常;
* 其他, 被查找元素对应的数组下标
* ---------------------------------------------------------------------*/
int32 BinarySearchRecur(DataType* plist, uint32 len, DataType item)
{
int32 min = 0, mid = 0, max = len-1; //定义为有符号类型
if(plist == NULL)
{
return -2;
}
else
{
while(min<=max)
{
mid = (min + max) / 2;
if(plist[mid] < item)
{
min = mid + 1;
BinarySearchRecur(&plist[min], max-min+1, item);
}
else if(plist[mid] > item)
{
max = mid - 1;
BinarySearchRecur(&plist[min], max-min+1, item);
}
else
{
return mid;
}
}
return -1;
}
}