c语言hash库排序函数 c语言hashmap

谁能帮忙写一个C语言的哈希排序?小女感激不尽~~1.选择合适c语言hash库排序函数的哈希函数H(key)=key%p(p为小于或等于哈希表长的最大质数)c语言hash库排序函数;
2.计算各个关键字的直接哈希函数值;
3.根据处理冲突的方法建立哈希表c语言hash库排序函数 , 并输出;
4.在哈希表中进行查找,输出查找的结果,以及所需和记录关键字比较的次数,并计算和输出在等概率情况下查找成功的平均查找长度 。
#includestdlib.h
#includestdio.h
#define NULL 0
typedef int KeyType;
typedef struct
{
KeyType key;
} ElemType;
int haxi(KeyType key,int m)/*根据哈希表长m , 构造除留余数法的哈希函数haxi*/
{
int i,p,flag;
for(p=m;p=2;p--)/*p为不超过m的最大素数*/
{
for(i=2,flag=1;i=p/2flag;i)
if(p%i==0)
flag=0;
if(flag==1)
break;
}
return key%p;/*哈希函数*/
}
void inputdata(ElemType **ST,int n)/*从键盘输入n个数据 , 存入数据表ST(采用动态分配的数组空间)*/
{
KeyType key;
int i;
(*ST)=(ElemType*)malloc(n*sizeof(ElemType));
printf("\nPlease input %d data:",n);
for(i=1;i=n;i)
scanf("%d",((*ST)[i].key));
}
void createhaxi(ElemType **HASH,ElemType *ST,int n,int m)/*由数据表ST构造哈希表HASH,n、m分别为数据集合ST和哈希表的长度*/
{
int i,j;
(*HASH)=(ElemType *)malloc(m*sizeof(ElemType));
for(i=0;im;i)
(*HASH)[i].key=NULL;/*初始化哈希为空表(以0表示空)*/
for(i=0;in;i)
{
j=haxi(ST[i].key,m);/*获得直接哈希地址*/
while((*HASH)[j].key!=NULL)
j=(j 1)%m;/*用线性探测再散列技术确定存放位置*/
(*HASH)[j].key=ST[i].key;/*将元素存入哈希表的相应位置*/
}
}
int search(ElemType *HASH,KeyType key,int m,int *time)/*在表长为m的哈希表中查找关键字等于key的元素,并用time记录比较次数,若查找成功,函数返回值为其在哈希表中的位置,否则返回-1*/
{
int i;
*time=1;
i=haxi(key,m);
while(HASH[i].key!=0HASH[i].key!=key)
{
i;
*time;
}
if(HASH[i].key==0)
return -1;
else
return i;
}
main()
{
ElemType *ST,*HASH;
KeyType key;
int i,n,m,stime,time;
char ch;
printf("\nPlease input nm(n=m)");/*输入关键字集合元素个数和哈希表长*/
scanf("%d%d",n,m);
inputdata(ST,n);/*输入n个数据*/
createhaxi(HASH,ST,n,m);/*创建哈希表*/
printf("\nThe haxi Table is\n");/*输出已建立的哈希表*/
for(i=0;im;i)
printf("]",i);
printf("\n");
for(i=0;im;i)
printf("]",HASH[i].key);
do/*哈希表的查找,可进行多次查找*/
{
printf("\nInput the key you want to search:");
scanf("%d",key);
i=search(HASH,key,m,time);
if(i!=-1)/*查找成功*/
{
printf("\nSuccess,the position is %d",i);
printf("\nThe time of compare is %d",time);
}
else/*查找失败*/
{
printf("\nUnsuccess");
printf("\nThe time of compare is %d",time);
}
printf("\nContiue(y/n):\n");/*是否继续查找yes or no*/
ch=getch();
}
while(ch=='y'||ch=='Y');/*计算表在等概率情况下的平均查找长度,并输出*/
for(stime=0,i=0;in;i)
{
search(HASH,ST[i].key,m,time);
stime =time;
}
printf("\nThe Average Search Length is %5.2f",(float)stime/n);
ch=getch();
}
C语言中的hash函数Hash , 一般翻译做"散列" , 也有直接音译为"哈希"的 , 就是把任意长度的输入(又叫做预映射 , pre-image),通过散列算法,变换成固定长度的输出 , 该输出就是散列值 。这种转换是一种压缩映射 , 也就是 , 散列值的空间通常远小于输入的空间,不同的输入可能会散列成相同的输出,而不可能从散列值来唯一的确定输入值 。简单的说就是一种将任意长度的消息压缩到某一固定长度的消息摘要的函数 。
HASH主要用于信息安全领域中加密算法,它把一些不同长度的信息转化成杂乱的128位的编码里 , 叫做HASH值. 也可以说 , hash就是找到一种数据内容和数据存放地址之间的映射关系 。Hash算法在信息安全方面的应用主要体现在以下的3个方面:文件校验、数字签名、鉴权协议
程程序实现
// 说明:Hash函数(即散列函数)在程序设计中的应用目标 ------ 把一个对象通过某种转换机制对应到一个
//size_t类型(即unsigned long)的整型值 。
// 而应用Hash函数的领域主要是 hash表(应用非常广)、密码等领域 。
// 实现说明:
// ⑴、这里使用了函数对象以及泛型技术,使得对所有类型的对象(关键字)都适用 。
// ⑵、常用类型有对应的偏特化,比如string、char*、各种整形等 。
// ⑶、版本可扩展,如果你对某种类型有特殊的需要,可以在后面实现专门化 。
// ⑷、以下实现一般放在头文件中 , 任何包含它的都可使用hash函数对象 。
//------------------------------------实现------------------------------------------------
#include string
using std::string;
inlinesize_thash_str(const char* s)
{
unsigned long res = 0;
for (; *s;s)
res = 5 * res*s;
returnsize_t(res);
}
template class Key
struct hash
{
size_toperator () (const Key k) const;
};
// 一般的对象,比如:vector queuestring ;的对象,需要强制转化
templateclass Key
size_thashKey::operator () (const Key k) const
{
size_tres = 0;
size_tlen = sizeof(Key);
const char* p = reinterpret_castconst char*(k);
while (len--)
{
res = (res1)^*p;
}
return res;
}
// 偏特化
template
size_thash string ::operator () (const string str) const
{
return hash_str(str.c_str());
}
typedef char* PChar;
template
size_thashPChar::operator () (const PChar s) const
【c语言hash库排序函数 c语言hashmap】{
return hash_str(s);
}
typedef const char* PCChar;
template
size_thashPCChar::operator () (const PCChar s) const
{
return hash_str(s);
}
template size_t hashchar::operator () (const char x) const { return x; }
template size_t hashunsigned char::operator () (const unsigned char x) const { return x; }
template size_t hashsigned char::operator () (const signed char x) const { return x; }
template size_t hashshort::operator () (const short x) const { return x; }
template size_t hashunsigned short::operator () (const unsigned short x) const { return x; }
template size_t hashint::operator () (const int x) const { return x; }
template size_t hashunsigned int::operator () (const unsigned int x) const { return x; }
template size_t hashlong::operator () (const long x) const { return x; }
template size_t hashunsigned long::operator () (const unsigned long x) const { return x; }
// 使用说明:
//
// ⑴、使用时首先由于是泛型,所以要加上关键字类型 。
//
// ⑵、其次要有一个函数对象 , 可以临时、局部、全局的,只要在作用域就可以 。
//
// ⑶、应用函数对象作用于对应类型的对象 。
//----------------------- hash函数使用举例 -------------------------
#include iostream
#include vector
#include string
using namespace std;
int main()
{
vectorstring vstr⑵;
vstr[0] = "sjw";
vstr[1] = "suninf";
hashstring strhash; // 局部函数对象
cout" Hash value: "strhash(vstr[0])endl;
cout" Hash value: "strhash(vstr[1])endl;
cout" Hash value: "hash vectorstring () (vstr)endl;
cout" Hash value: "hashint() (100)endl; // hashint() 临时函数对象
return 0;
}
C语言数据结构希尔排序void main()
{
datatype R[MAXNUM];
int d[6]=[50,25,12,6,3,2,1];
for(int i=0;iMAXNUM;i)
scanf("%d",R[i].key);
ShellSort(R,MAXNUM,d,6);
for(int i=0;iMAXNUM;i)
printf("%d",R[i].key);
}
希尔排序(c语言)void ShellSort(int r[],int n)//希尔排序
{
for(int gap=n/2;gap=1;gap=gap/2)//以增量为d进行直接插入排序
{
CountCompare[1];
for(int i=d 1;i=n;i)//将r[i]插入到所属c语言hash库排序函数的子序列中
{
r[0]=r[i];//暂存被插入记录
CountMove[1];
for(int j=i-d;j0r[0]r[j];j=j-gap)
{
r[j d]=r[j];//记录后移gap个位置c语言hash库排序函数,保证仍在同一个子序列
CountCompare[1];
CountMove[1];
}
r[j gap]=r[0];
CountMove[1];
}
for(int k=1;k=n;k)
coutr[i]"";
}
}
//主程序就麻烦自己写c语言hash库排序函数了
关于c语言hash库排序函数和c语言hashmap的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读