-->
当前位置:首页 > 题库 > 正文内容

程序填空题:折半查找算法

Luz4年前 (2021-05-10)题库3833
本题要求实现折半查找的递归查找操作。 例如对于下图的有序表:

![有序表.png](~/4f305a4e-1418-45db-bf8a-ff6f2904a2f5.png)
### 输入样例:
```
2
21 70
```
### 输出样例:

```out
21 is found
70 is not found
```
###程序如下:
```c++
#include
#include
typedef int KeyType;
typedef struct {
KeyType *data; /*表基址*/
int length; /*表长*/
}SSTable;
void CreatSSTable(SSTable *ST);/*有序表创建,由裁判实现,细节不表*/
int BiSearch(SSTable ST,KeyType e,int low,int high);
int main()
{
SSTable ST;
int n,result,i;
KeyType e;
CreatSSTable(&ST);
scanf("%d",&n);
for( i=0;i {
scanf("%d",&e);
result = BiSearch(ST,e,1,ST.length);
if(result) printf("%d is found\n",e);
else printf("%d is not found\n",e);
}
return 0;
}
int BiSearch(SSTable ST,KeyType e,int low,int high)
{
int mid;
if(@@[low>high](2)) return 0;
mid=(low+high)/2;
if(ST.data[mid]==e) return mid;
if(e else @@[ return BiSearch(ST,e, mid+1,high) ](2);
}
```






答案:
第1空:low>high

第2空: BiSearch(ST,e, low,mid-1)

第3空: return BiSearch(ST,e, mid+1,high)

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。