顺序查找
算法:
int SeqSearch(Seq array[],int n,DataType key)
{
int i=n;
array[0].key=key;
while(array[i].key!=key) i--;
return i;
}
完整代码:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
typedef int DataType;
#define MAXSIZE 100
typedef struct seq
{
DataType key;
}Seq;
int SeqSearch(Seq array[],int n,DataType key)
{
int i=n;
array[0].key=key;
while(array[i].key!=key) i--;
return i;
}
int main()
{
int n;
int key;
Seq q[MAXSIZE];
cout<<"请输入数组长度:";
cin>>n;
cout<<"请输入数据:";
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
q[i].key=x;
}
cout<<"请输入要查找的值:";
cin>>key;
if(SeqSearch(q,n,key))
cout<<"查命中,"<<key<<"的下标为:"<<SeqSearch(q,n,key)<<endl;
else cout<<"查不命中"<<endl;
system("pause");
return 0;
}
折半查找
算法:
//非递归实现折半查找
int HalfSearch(DataType array[],DataType n,DataType key)
{
int low=1;
int high=n;
while(low<=high)
{
int mid=(low+high)/2;
if(key<array[mid]) high=mid-1;
else if(key>array[mid]) low=mid+1;
else return mid;
}
return 0;
}
//递归实现实现折半查找
int BinSearch(DataType array[],DataType low,DataType high,DataType key)
{
if(low>high) return 0;
int mid=(low+high)/2;
if(key<array[mid]) return BinSearch(array,low,mid-1,key);
else if(key>array[mid]) return BinSearch(array,mid+1,high,key);
else return mid;
}
完整代码:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
#define MAXSIZE 100
typedef int DataType;
//非递归实现折半查找
int HalfSearch(DataType array[],DataType n,DataType key)
{
int low=1;
int high=n;
while(low<=high)
{
int mid=(low+high)/2;
if(key<array[mid]) high=mid-1;
else if(key>array[mid]) low=mid+1;
else return mid;
}
return 0;
}
//递归实现实现折半查找
int BinSearch(DataType array[],DataType low,DataType high,DataType key)
{
if(low>high) return 0;
int mid=(low+high)/2;
if(key<array[mid]) return BinSearch(array,low,mid-1,key);
else if(key>array[mid]) return BinSearch(array,mid+1,high,key);
else return mid;
}
int main()
{
int n;
int key1,key2;
cout<<"请输入数组长度:";
cin>>n;
int a[MAXSIZE];
cout<<"自动建立数组:";
for(int i=1;i<=n;i++)
{
a[i]=10*i;
cout<<a[i]<<" ";
}
cout<<endl;
cout<<"非递归实现:"<<endl;
cout<<"请输入要查找的值:";
cin>>key1;
if(HalfSearch(a,n,key1))
cout<<"查命中,"<<key1<<"的下标为:"<<HalfSearch(a,n,key1)<<endl;
else cout<<"查不命中"<<endl;
cout<<"递归实现:"<<endl;
cout<<"请输入要查找的值:";
cin>>key2;
if(BinSearch(a,1,n,key2))
cout<<"查命中,"<<key2<<"的下标为:"<<BinSearch(a,1,n,key2)<<endl;
else cout<<"查不命中"<<endl;
system("pause");
return 0;
}
索引查找
算法:
int IndexSearch(elem e[], DataType key, int n, index idx[], int idx_length)
{
int k = 0;
//采用顺序查找的方法在索引表里找到关键字所在的块
while (k <= idx_length && key > idx[k].key)
k++;
if (k > idx_length)
return -1;
else
{
//采用顺序查找的方法从块中查找关键值
int i = idx[k].low;
while (i <= idx[k].high && e[i].key != key)
{
i++;
}
if (i > idx[k].high)
return -1;
else
return i;
}
}
完整代码:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
#define MAX 16
typedef int DataType;
typedef struct elem
{
DataType key; //关键字
} Seq;
//索引结构
struct index
{
DataType key; //索引值
long low; //起始位置
long high; //终止位置
};
int IndexSearch(elem e[], DataType key, int n, index idx[], int idx_length)
{
int k = 0;
//采用顺序查找的方法在索引表里找到关键字所在的块
while (k <= idx_length && key > idx[k].key)
k++;
if (k > idx_length)
return -1;
else
{
//采用顺序查找的方法从块中查找关键值
int i = idx[k].low;
while (i <= idx[k].high && e[i].key != key)
{
i++;
}
if (i > idx[k].high)
return -1;
else
return i;
}
}
int main()
{
Seq linelist[MAX] = {
8, 20, 13, 17,
40, 42, 45, 32,
49, 58, 50, 52,
67, 79, 78, 80
};
int n = sizeof(linelist) / sizeof(elem);
DataType key;
//建立索引表
index index_table[4] = {{20,0,3}, {45,4,7}, {58,8,11}, {80,12,15}};
int idx_length = sizeof(index_table) / sizeof(index);
printf("索引表中的元素为:\n");
for (int i = 0; i < n; i++)
{
cout << linelist[i].key << " ";
}
cout << endl
<< endl;
cout << "请输入要查找的值:";
cin >> key;
if (IndexSearch(linelist, key, n, index_table, idx_length) != -1)
{
printf("\n查命中,关键字%d在线性表中的位置下标为%d\n\n", key, IndexSearch(linelist, key, n, index_table, idx_length));
}
else
cout << "查不命中" << endl;
system("pause");
return 0;
}
哈希查找
算法:
void BuildHash(int array[],int hash[],int SearchLength[])
{
for(int i=0;i<ARRAY_SIZE;i++)
{
int x=array[i]%KEY;
if(hash[x] == -1)
{
hash[x]=array[i];
SearchLength[x]=1;
}
else
{
int k=1;
while(x<HASH_SIZE && hash[x] != -1)
{
x++;
k++;
}
if(x<HASH_SIZE && hash[x] == -1)
{
hash[x]=array[i];
SearchLength[x]=k;
}
}
}
}
完整代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
#define KEY 11
#define ARRAY_SIZE 9
#define HASH_SIZE 13
void BuildHash(int array[],int hash[],int SearchLength[])
{
for(int i=0;i<ARRAY_SIZE;i++)
{
int x=array[i]%KEY;
if(hash[x] == -1)
{
hash[x]=array[i];
SearchLength[x]=1;
}
else
{
int k=1;
while(x<HASH_SIZE && hash[x] != -1)
{
x++;
k++;
}
if(x<HASH_SIZE && hash[x] == -1)
{
hash[x]=array[i];
SearchLength[x]=k;
}
}
}
}
int main()
{
int array[ARRAY_SIZE]={16,28,41,22,37,79,30,19,38};
int hash[HASH_SIZE];
int SearchLength[HASH_SIZE];
float ASL=0.0;
memset(hash,-1,sizeof(hash));
memset(SearchLength,0,sizeof(SearchLength));
cout<<"线性表为:";
for(int i=0;i<ARRAY_SIZE;i++) cout<<array[i]<<" ";
cout<<endl;
BuildHash(array,hash,SearchLength);
cout<<"散列表为:";
for(int i=0;i<HASH_SIZE;i++) cout<<hash[i]<<" ";
cout<<endl;
cout<<"查找长度为:";
for(int i=0;i<HASH_SIZE;i++)
{
cout<<SearchLength[i]<<" ";
ASL+=SearchLength[i];
}
cout<<endl;
cout<<"平均查找长度(ASL)为:"<<ASL/ARRAY_SIZE<<endl;
system("pause");
return 0;
}