双重散列法http://blog.csdn.net/zixiawzm/article/details/6746946

#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>

#define slot_size 100000 //散列槽的大小
#define arr_size 80000 //动态关键字集合
#define min_size 0 //动态关键字集合的最小值
#define max_size 999
#define total_size 999999 //动态关键字集合的最大值
#define NIL -1
#define DELE -2
using namespace std;

long* arr_set;
long link_hash[slot_size];
long suc_count=0;
long unsuc_count=0;

/*
* 第i次探查的序列散列函数
*/
long hash_function(long key,long i)
{
return (key%700+i*(key%(701-1)))%slot_size;
}

/*
* 产生不重复的自定义范围的随机数
*/
long* ran_arr(long size, long min=0, long max=999)
{
if(max<=min)
return NULL;
long* arr;
long up_th=0;
long down_th=0;
arr=new long[size];
srand((unsigned)time(NULL));
for(long i=0; i<size; i++)
{
long check=1;
while(check)
{
up_th=rand()*(max-min)/32767+min;
down_th=rand()*(max-min)/32767+min;
arr[i]=up_th*(max+1)+down_th;
long j=0;
while(j<i)
{
if(arr[i]==arr[j])
{
j=0;
break;
}
j++;
}
if(j==i)
check=0;
}
}
return arr;
}

/*
* 打印数组函数
*/
void print_arr(long* set,long size)
{
for(long i=0;i<size;i++)
{
cout<<set[i]<<endl;
}
}

/*
* 插入函数
*/
bool hash_insert(long k)
{
long j=0;
for(long i=0; i<slot_size; i++)
{
j=hash_function(k,i);
if(link_hash[j]==NIL)
{
link_hash[j]=k;
return true;
}
}
return false;
}

/*
* 查找函数
*/
bool hash_find(long k)
{
long j=0;
for(int i=0; i<slot_size; i++)
{
j=hash_function(k,i);
if(link_hash[j]==k)
return true;
else
if(link_hash[j]==NIL)
return false;
}
return false;
}

/*
* 删除函数
*/
bool hash_delete(long k)
{
long j=0;
for(int i=0; i<slot_size; i++)
{
j=hash_function(k,i);
if(link_hash[j]==k)
{
link_hash[j]=DELE;
return true;
}
else
{
if(link_hash[j]==NIL)
return false;
}
}
return false;
}

/*
* 打印散列表的函数
*/
void print_hash(long start,long end)
{

long count=0;
for(long j=start;j<end;j++)
{

if(link_hash[j]==NIL)
{
cout<<j<<"[NIL]"<<" ";
}
else if(link_hash[j]==DELE)
{
cout<<j<<"[DEL]"<<" ";
}
else
{
cout<<j<<"["<<link_hash[j]<<"] ";
}
count++;
if(count==4)
{
count=0;
cout<<endl;
}

}
cout<<endl;
return;
}


/*
* 主函数
*/
int main(int argc, char* argv[])
{


//cout<<"please input the size of the key that you want to store:";
//cin>>arr_size;


//print_arr(arr_set,arr_size);

//初始化散列表的槽
for(int d=0;d<5;d++)
{
arr_set=ran_arr(arr_size-d*10000,min_size,max_size);//to generate arr_size from 1 to 1000 random number
for(long n=0;n<slot_size;n++)
{
link_hash[n]=NIL;
}
cout<<"befor the insertion:"<<endl<<endl;
print_hash(200,232);

//插入操作
DWORD insert_start = GetTickCount();
for(long m=0; m<arr_size-d*10000; m++)
{
hash_insert(arr_set[m]);
}
DWORD insert_time=GetTickCount() - insert_start;
cout<<"the size of n is: "<<arr_size-d*10000<<endl;
cout<<"the size of m is: "<<slot_size<<endl;
cout<<"the value of a=n/m is: "<<float(arr_size-d*10000)/float(slot_size)<<endl;
cout<<"the total insert running time is: "<<insert_time<<" milliseconds "<<endl;
cout<<"the average insert time is: "<<float(insert_time)/float(arr_size)<<" milliseconds "<<endl<<endl;
cout<<"***********************************************************"<<endl<<endl;
cout<<"after the insertion:"<<endl<<endl;
print_hash(200,232);

//查找操作
DWORD find_start=GetTickCount();
for(long n=0; n<arr_size-d*10000; n++)
{
if(hash_find(arr_set[n]))
{
suc_count++;
}
else
{
unsuc_count++;
}

}
DWORD find_time=GetTickCount()-find_start;
cout<<"the total finding running time is: "<<find_time<<" milliseconds "<<endl;
cout<<"the average finding runnig time is: " <<float(find_time)/float(arr_size)<<" milliseconds "<<endl;
cout<<"the success finding count is :"<<suc_count<<endl;
cout<<"the unsuccess finding count is :"<<unsuc_count<<endl<<endl;
cout<<"***********************************************************"<<endl<<endl;
suc_count=unsuc_count=0;//计数清零;
//删除操作
DWORD delete_start=GetTickCount();
for(long j=0; j<arr_size-d*10000; j++)
{
if(hash_delete(arr_set[j]))
{
suc_count++;
}
else
{
unsuc_count++;
}
}
DWORD delete_time=GetTickCount()-delete_start;
cout<<"the total deleting running time is: "<<delete_time<<" milliseconds "<<endl;
cout<<"the average deleting runnig time is: " <<float(delete_time)/float(arr_size)<<" milliseconds "<<endl;
cout<<"the success deleting count is :"<<suc_count<<endl;
cout<<"the unsuccess deleting count is :"<<unsuc_count<<endl<<endl;
suc_count=unsuc_count=0;//计数清零;
cout<<"***********************************************************"<<endl<<endl;
cout<<"after the deletion:"<<endl<<endl;
print_hash(200,232);
}

int a;
cin>>a;

return 0;
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。