zoukankan      html  css  js  c++  java
  • 双重散列法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;
    }

  • 相关阅读:
    Selenium+Pytest自动化测试框架实战
    WPF性能优化经验总结
    C#跨窗体调用控件
    C# lock
    硬实时系统,到底多硬才算Hard Real Time System
    [GPIO]推荐一种超简单的硬件位带bitband操作方法,让变量,寄存器控制,IO访问更便捷,无需用户计算位置
    【STM32F407的DSP教程】第50章 STM32F407的样条插补实现,波形拟合丝滑顺畅
    实战技能分享,如何让工程代码各种优化等级通吃,含MDK AC5,AC6,IAR和GCC
    【深入探讨】DMA到底能不能起到加速程序执行的作用,DMA死等操作是否合理,多个DMA数据流同时刷是否处理过来
    《安富莱嵌入式周报》第238期:2021.11.012021.11.07
  • 原文地址:https://www.cnblogs.com/kimmychul/p/3951480.html
Copyright © 2011-2022 走看看