折半查找 帮忙找错
源码如下:#include<stdio.h>
#include<stdlib.h>
#define MAX_LENGTH 100
typedef int KeyType;
typedef struct
{
int key;
}ElemType;
typedef struct
{
ElemType elem[MAX_LENGTH];
int length;
}SSTable;
int Search_Bin(SSTable ST,KeyType key)
{
int low,high,mid;
low = 1;high = ST.length;
while(low <=high)
{
mid = (low+high)/2;
if(key ==ST.elem[mid].key)
return mid;
else
if(key<ST.elem[mid].key)
high = mid-1;
else
low=mid+1;
}
return 0;
}
void main()
{
int i,result;
SSTable ST;
KeyType key;
printf("please input length:");
scanf("%d",&ST.length);
for(i=1;i<=ST.length;i++)
{
printf("please input ST.elem:");
scanf("%d",&ST.elem[i]);
}
printf("please input keyword:");
scanf("%d",&key);
result=Search_Bin(ST,key);
if(result==0)
printf("Don't find\n");
else
printf("Find the key,the position is %d\n",result);
}