一、折半查找
#include<iostream>
using namespace std;
template<typename T>
T binartsearch(T a[], T m,int left,int right)
{
if (left > right)return -1;
int base = (left+ right) / 2;
if (a[base] == m)return base;
if (a[base] > m)return binartsearch(a, m, left, base-1);
if (a[base] < m)return binartsearch(a, m, base+1, right);
}
int main()
{
int a[9] = { 1,2,3,4,5,6,7,8,9 };
int temp[9];
cout<<binartsearch(a,9,0,8);
return 0;
}
二、插值查找
#include<iostream>
using namespace std;
template<typename T>
T binartsearch(T a[], T value,int left,int right)
{
if (left > right)return -1;
int base = left + (value - a[left])/(a[right]-a[left])*(right-left);
if (a[base] == value)return base;
if (a[base] > value)return binartsearch(a, value, left, base-1);
if (a[base] < value)return binartsearch(a, value, base+1, right);
}
int main()
{
int a[9] = { 1,2,3,4,5,6,7,8,9 };
int temp[9];
cout<<binartsearch(a,9,0,8);
return 0;
}
三、斐波那契查找
#include<iostream>
using namespace std;
#define fibmax 100
void profib(int a[], int len)
{
a[0] = 1;
a[1] = 1;
for (int i = 2; i < len; i++)
{
a[i] = a[i - 2] + a[i - 1];
}
}
int fibonasearch(int a[], int len, int value)
{
int fib[fibmax];
profib(fib,fibmax);
int high = len - 1;
int low = 0;
int k = 0;
while (high > fib[k] - 1)
{
k++;
}
int* temp;
temp = new int[fib[k] - 1];
memcpy(temp, a, len * sizeof(int));
for (int i = len; i < fib[k] - 1; i++)
{
temp[i] = a[high];
}
int mid;
while (low < high)
{
mid = low + fib[k - 1] - 1;
if (temp[mid] == value)
{
if (mid <= len - 1)return mid;
else return len - 1;
}
if (temp[mid] > value)
{
high = mid - 1;;
k = k - 1;
}
if (temp[mid] < value)
{
low = mid + 1;
k = k - 2;
}
}
return -1;
}
int main()
{
int data[] = { 1,3,5,7,9,11,13,15,17,19,21 };
int index = fibonasearch(data, 11, 19);
cout << index;
return 0;
}
四、分块查找
#include<iostream>
using namespace std;
struct index
{
int key;
int start;
int end;
}newindex[4];
int blocksearch(int a[], int key,int len)
{
int low, high, mid;
low = 1;
high = len;
while (low <= high)
{
mid = (low + high)/2;
if (key <= newindex[mid].key)
{
if (key <= newindex[mid - 1].key)
{
high = mid - 1;
}
else {
for (int i = newindex[mid].start; i <= newindex[mid].end; i++)
{
if (key == a[i])
{
return (i + 1);
}
}
return 0;
}
}
else {
low = mid + 1;
}
}
}
int main()
{
int i;
int a[] = { 22,12,13,8,9,20,33,42,44,38,24,48,60,58,74,49,86,53 };
newindex[1].key = 22; newindex[1].start = 0; newindex[1].end = 5;
newindex[2].key = 48; newindex[2].start = 6; newindex[2].end = 11;
newindex[3].key = 86; newindex[3].start = 12; newindex[3].end = 17;
int key;
cin >> key;
i = blocksearch(a, key,3);
cout << i;
}
五、哈希查找
#include<iostream>
using namespace std;
#define max 15
int Hash(int key)
{
return key % max;
}
void InitHashTable(int data[],int a[],int len)
{
for (int i = 0; i < max; i++)
{
data[i] = -1;
}
int index;
for (int i = 0; i < len; i++)
{
index = Hash(a[i]);
if (data[index] != -1)
{
index = (index + 1) % max;
while(data[index]!=-1&& index !=Hash(a[i]))index = (index + 1) % max;
}
data[index] = a[i];
}
}
int searchHash(int data[], int key,int len)
{
int temp[max];
InitHashTable(temp,data,len);
cout << "哈希表为:";
for (int i = 0; i < max; i++)
{
cout << temp[i] << " ";
}
cout << endl;
int h = Hash(key);
if (temp[h] == -1)return -1;
else if (temp[h] == key)return h;
else {
for (int i = 1; i < max; i++)
{
int h1 = (h + i) % max;
if (temp[h1] == -1)return -1;
else if (temp[h1] == key)return h1;
}
return -1;
}
}
int main()
{
int data[] = { 1,3,5,7,9,11,13,15,17,19,21 };
int a;
cin >> a;
int index = searchHash(data, a, 11);
if (index == -1)cout << "不存在此元素" << endl;
else cout << index;
return 0;
}