#include "stdafx.h"
#include<iostream>
#include <time.h>
using namespace std;
#pragma warning(disable:4996)
void HalfSearch(int num[], int left, int right, int nData)
{
int Mid = (right + left) / 2;
if (left == right){
return;
}
if (num[Mid] == nData){
printf("%d\n", Mid);
return;
}
if (num[Mid] >= nData){
HalfSearch(num, left, Mid, nData);
}
else{
HalfSearch(num, Mid, right, nData);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int num[9] = {1, 6, 9 , 12, 18, 20, 40, 55, 61};
HalfSearch(num, 0, 9, 12);
return 0;
}
二分查找又称折半查找,优点是比较次数少,查找速度快,平均性能好;其缺点是
要求待查表为有序表
,且插入删除困难。因此,折半查找方法适用于不经常变动而查找频繁的有序列表
折半查找
最新推荐文章于 2024-04-08 18:49:50 发布