【数据结构作业】实现任意三种静态或动态查找

来源:互联网 发布:波士顿矩阵图的优缺点 编辑:程序博客网 时间:2024/06/03 19:49

这里是最简单的二分,分块和Hash表

#include <algorithm>#include <bitset>#include <cassert>#include <climits>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <deque>#include <iomanip>#include <iostream>#include <map>#include <numeric>#include <queue>#include <set>#include <stack>#include <string>#define INF 0x3f3f3f3fusing namespace std;typedef long long ll;#define MAXL 100#define MAXI 20#define HASH_SIZE 100//哈希表 链式存储法解决冲突typedef struct Hash_Table{    int data;    int pos;    Hash_Table* next;}*Hash_TablePtr;int Hash(int key);int Search(Hash_TablePtr hash, int key);//void randperm(int a[],int Num){    vector<int> temp;    for (int i = 0; i < Num; ++i)    {        temp.push_back(i + 1);    }    random_shuffle(temp.begin(), temp.end());    for (int i = 0; i < 100; i++)    {        a[i]=temp[i];    }}//二分查找int BinarySearch(int a[],int st,int ed,int key){    int low=st,high=ed;    int mid=0;    while(high-low>=0){        mid=low+(high-low)/2;        if(a[mid]>key){            high=mid-1;        }        else if(a[mid]<key){            low=mid+1;        }        else            return mid;    }    return 0;}void search_1() {    srand((unsigned int)time(0));    //BinarySearch    int a[100];    randperm(a,150);    /*for (int i = 0; i < 100; i++) {        a[i] = rand() % 200;    }*/    sort(a, a + 100);    for(int i=0;i<100;i++)        cout<<a[i]<<" ";    cout<<endl;        int t = 20;        while (t--) {            int s = rand() % 100;            int status = BinarySearch(a, 0, 99, s);            cout << "Case " << 20 - t << ":" << endl;            if (status) {                cout << s << "    位置:" << status << "  ";                cout << "元素:" << a[status] << endl;            } else                cout << s << "     不存在的!" << endl;        }}//分块查找typedef int KeyType;typedef char InfoType[10];typedef struct{    KeyType key;                //KeyType为关键字的数据类型    InfoType data;              //其他数据} NodeType;typedef NodeType SeqList[MAXL]; //顺序表类型typedef struct{    KeyType key;            //KeyType为关键字的类型    int link;               //指向对应块的起始下标} IdxType;typedef IdxType IDX[MAXI];  //索引表类型int IdxSearch(IDX I,int m,SeqList R,int n,KeyType k){    int low=0,high=m-1,mid,i;    int b=n/m;              //b为每块的记录个数    while (low<=high)       //在索引表中进行二分查找,找到的位置存放在low中    {        mid=(low+high)/2;        if (I[mid].key>=k)            high=mid-1;        else            low=mid+1;    }    //应在索引表的high+1块中,再在线性表中进行顺序查找    i=I[high+1].link;    while (i<=I[high+1].link+b-1 && R[i].key!=k) i++;    if (i<=I[high+1].link+b-1)        return i+1;    else        return 0;}void search_2(){    srand((unsigned int)time(0));    int i,n=25,m=5,j;    SeqList R={0};    IDX I= {{14,0},{34,5},{66,10},{85,15},{100,20}};//索引表    KeyType a[]= {8,14,6,9,10,22,34,18,19,31,40,38,54,66,46,71,78,68,80,85,100,94,88,96,87};    int t=20;    for (i=0; i<n; i++)        R[i].key=a[i];    while(t--){        cout<<"Case "<<20-t<<": "<<endl;        KeyType x=rand()%100+1;        j=IdxSearch(I,m,R,n,x);        if (j!=0)            printf("     %d是第%d个数据\n",x,j);        else            printf("     未找到%d\n",x);    }}//int Hash(int key){    return key % 11;}int Search_hash(Hash_TablePtr hash, int key){    int adress = Hash(key);    if (hash[adress].data == -1)        return 0;    else if (hash[adress].data == key)        return adress;    else{        Hash_TablePtr temp = &hash[adress];//哈希表开始        while (temp != NULL){            temp = temp->next;            if (temp == NULL)                return 0;            else if (temp->data == key)                return temp->pos;        }        return 0;    }}void search_3(){    Hash_Table hash[HASH_SIZE];    int i, search;    int temp[HASH_SIZE] = {1,2,3,4,5,7,9,10,11,12,13,14,15,17,18,19,20,22,23,24,26,27,29,30,31,33,36,37,39,40,                           42,43,44,46,47,49,50,51,52,58,60,61,62,63,64,65,66,68,69,70,71,74,75,76,77,78,79,80,82,83,86,                           87,88,90,91,95,96,98,100,103,104,105,106,107,108,109,110,111,112,114,116,117,119,120,121,122,                           123,124,125,128,130,133,134,135,138,139,141,142,143,148};    for (i = 0; i < HASH_SIZE; i++){        hash[i].data = -1;        hash[i].pos=0;        hash[i].next = NULL;    }    for (i = 0; i < HASH_SIZE; i++){        int x, y;        x = temp[i];        y = Hash(x);        Hash_TablePtr pre;        if (hash[y].data == -1)            hash[y].data = x;        else{            Hash_TablePtr p = (Hash_TablePtr)malloc(sizeof(Hash_Table));            p->data = x;            p->pos=i+1;            p->next = NULL;            pre = &hash[y];            while (pre->next != NULL)                pre = pre->next;            pre->next = p;        }    }    int t=10;    while(t--){        printf("Case %d:  ",10-t);        search=rand()%100+1;        int pp=Search_hash(hash, search);        if (pp)            printf("找到%d  位置:%d\n",search,pp);        else            printf("没找到\n");    }}int main(){    int choice=0;    do{        cout<<"1、二分查找  2、分块查找  3、哈希表"<<endl;        cin>>choice;        switch (choice){            case 1: search_1();            case 2: search_2();            case 3: search_3();            case 0: break;            default: cout<<"输入有错误!"<<endl;        }    }while(choice);    return 0;}
原创粉丝点击