数据结构-查找-顺序查找(1)时间复杂度(n+1)/2

来源:互联网 发布:彩蝶排课软件 编辑:程序博客网 时间:2024/06/05 03:02

1.代码区

#include <iostream>
#define MAX 111
using namespace std;
typedef struct{
int key;//关键字
       //other data
} ElemType;
typedef struct{
ElemType *R;//如上
int length;//顺序表长度
} Sstable;
int searchSeq(Sstable st, int key);
int main(){
//定义一个顺序表并开辟一个足够的数组空间
Sstable st;
st.R = new ElemType[MAX];
//输入一段无序的序列
cout << "please input sequence length:\n";
cin >> st.length;
cout << "please input sequence elemts:\n";
for(int i = 0; i < st.length; i++){
cin >> st.R[i].key;
}
//调用查找函数
int key;
cout << "please input the number you want to find:\n";
cin >> key;
int index = searchSeq(st,key);
//输出查找结果
if(index != 0){
cout << "The number of number subscript to be found is:";
cout << index << endl;
}
else{
cout << "No answer!" << endl;
}
return 0;
}
int searchSeq(Sstable st, int key){
int i;
st.R[0].key = key;
for(i = st.length; st.R[i].key != key; i--);
return i;
}


2.结果区


阅读全文
0 0
原创粉丝点击