#include<iostream> using namespace std; typedef struct { KeyType key; InforType otherinfo; }Elemtype;/*数组的内容*/ typedef struct { Elemtype *R;/*定义数组的首地址*/ int length; }SSTable; /*顺序查找*/ int Search_Seq(SSTable ST,int key) { for(int i=ST.length;i>=1;--i) if(ST.R[i].key==key) return i; return 0; } /*设置监视哨的顺序查找*/ int Search_Seq(SSTable ST,KeyType key) { ST.R[0].key=key;/*对数组设置时,前提为数组小标为0的位置内容为空,从1开始,于是数组中所查遇到则返回下标,如未遇到则返回0*/ for(int i=ST.length;ST.R[i].key!=key;--i) return i; } /*折半查找*/ int Search_Bin(SSTable ST,KeyType key) { low=1; high=ST.length; while(low<=high) { mid=(low+high)/2; if(key==ST.R[mid].key) return mid; else if(key<ST.R[mid].key) high=mid-1; else low=mid+1; } } /*递归*/ int Search_Bin(SSTable ST,KeyType key,int low,int high) { if(low>high) return 0; mid=(low+high)/2; if(key==ST.R[mid]) return mid; else if(key<=ST.R[mid]) return Search_Bin(ST,key,mmid+1,high); else return Search_Bin(ST,key,low,mid-1); }
0.0分
2 人评分