C语言编程:折半法,在15个数中查找X,若无,则输出“无此数”

来源:百度知道 编辑:UC知道 时间:2024/06/07 09:57:31

//Search_Seq.cpp
//This function is to find location of the inputed element in SSTbale
# include <malloc.h>
# include <iostream.h>

# include <conio.h>
# define MAX_LENGTH 100
typedef int KeyType;

typedef struct //define structure SSTable
{ KeyType *elem;
int length;
}SSTable;

int Search_Seq(SSTable ST,KeyType key) //Search_Seq function
{ int mid,low=1,high=ST.length;
while(low<=high)
{ mid=(low+high)/2;
if(key==ST.elem[mid])
return (mid);
else if(key<ST.elem[mid])
high=mid-1;
else
low=mid+1;
}
return (0);
}

void main() //main() function
{ int i,key;
SSTable ST;
ST.elem=(KeyType *)malloc(sizeof(KeyType));
cout<<endl<<endl<<"Search_Seq.cpp";
cout<<endl<<"==============";