双重散列法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;
}

时间: 2024-08-26 07:53:56

双重散列法http://blog.csdn.net/zixiawzm/article/details/6746946的相关文章

图像处理算法2——Otsu最佳阈值分割法http://blog.csdn.net/xiaqunfeng123/article/details/17121195

http://blog.csdn.net/xiaqunfeng123/article/details/17121195Otsu法是1979年由日本大津提出的.该方法在类间方差最大的情况下是最佳的,即统计鉴别分析中所用的度量.Otsu方法有一个重要的特性,就是它完全以在一幅图像的直方图上执行计算为基础,而直方图是很容易得到的一维阵列. 具体的公式推理及公式细节就不说了,详见 Conzalez 那本书,我是第三版的,在P.479——P.482 上面. 给出具体步骤如下: 1.计算输入图像的直方图,并

散列(2)线性探测法和双重散列法

接上篇 散列的简要描述和链地址法 解决散列冲突的方法: 1. 线性探测法 如果我们能够预测将要存入表中元素的数目,而且我们有足够的内存空间可以容纳带有空闲空间的所有关键字,那么使用链地址法是不值得的.我们依靠空的存储空间解决冲突:设计表长M大于元素数目N,开放地址法,最简单的开放地址法是线性探测法: 初始化 该符号表的实现将元素保存到大小是元素个数两倍的散列表中. void HashTableInit(int max) { N = 0; M = 2*max; hash_table = new I

Cassandra研究报告-http://blog.csdn.net/zyz511919766/article/details/38683219/

转自http://blog.csdn.net/zyz511919766/article/details/38683219/ 1基本安装 1.1在基于RHEL的系统中安装Cassandra 1.1.1必要条件 Ø  YUM包管理器 Ø  Root或sudo权限 Ø  JRE6或者JRE7 Ø  JNA(Java native Access)(生产环境需要) 1.1.2步骤 Ø  安装配置JRE(略) Ø  添加软件包仓库到YUM的软件库 将以下内容添加进/etc/yum.repos.d/datas

Javascript 设计模式 单例 http://blog.csdn.net/lmj623565791/article/details/30490955/

转载请标明出处:http://blog.csdn.NET/lmj623565791/article/details/30490955 一直很喜欢Js,,,今天写一个Js的单例模式实现以及用法. 1.单例模式的写法 单例模式写法相当简单: [javascript] view plain copy var singleTon = { m1: "memeber first ", m2: "memeber second ", f1: function () { consol

装载 - POJ分类很好很有层次感 from http://blog.csdn.net/zzycsx/article/details/49103451

from http://blog.csdn.net/zzycsx/article/details/49103451 OJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094)初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.     

mybaits入门(含实例教程和源码) http://blog.csdn.net/u013142781/article/details/50388204

前言:mybatis是一个非常优秀的存储过程和高级映射的优秀持久层框架.大大简化了,数据库操作中的常用操作.下面将介绍mybatis的一些概念和在eclipse上的实际项目搭建使用. 一.mybatis的概念介绍 1.1.背景介绍 MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索.MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old 

系统权限管理设计 (转:http://blog.csdn.net/chexlong/article/details/37697555)

权限设计(转:http://blog.csdn.net/chexlong/article/details/37697555) 1. 前言: 权限管理往往是一个极其复杂的问题,但也可简单表述为这样的逻辑表达式:判断"Who对What(Which)进行How的操作"的逻辑表达式是否为真.针对不同的应用,需要根据项目的实际情况和具体架构,在维护性.灵活性.完整性等N多个方案之间比较权衡,选择符合的方案. 2. 目标: 直观,因为系统最终会由最终用户来维护,权限分配的直观和容易理解,显得比较重

RTP协议分析(转自:http://blog.csdn.net/bripengandre/article/details/2238818)

RTP协议分析 第1章.     RTP概述 1.1.  RTP是什么 RTP全名是Real-time Transport Protocol(实时传输协议).它是IETF提出的一个标准,对应的RFC文档为RFC3550(RFC1889为其过期版本).RFC3550不仅定义了RTP,而且定义了配套的相关协议RTCP(Real-time Transport Control Protocol,即实时传输控制协议).RTP用来为IP网上的语音.图像.传真等多种需要实时传输的多媒体数据提供端到端的实时传输

http://blog.csdn.net/LANGXINLEN/article/details/50421988

GitHub上史上最全的Android开源项目分类汇总 今天在看博客的时候,无意中发现了 @Trinea在GitHub上的一个项目 Android开源项目分类汇总, 由于类容太多了,我没有一个个完整地看完,但是里面介绍的开源项目都非常有参考价值,包括很炫的界面特效设计.个性化控件.工具库.优秀的Android 开源项目.开发测试工具.优秀个人和团体等.可以这样说,每一位Andorid开发人员都能从中找到一个或多个适用自己项目的解决方案,消化吸收并加以利 用,可以为自己的APP增色不少.文章最后还