三大查找算法总结:二分查找,分块查找和哈希表查找。

来源:互联网 发布:外貌协会知乎 编辑:程序博客网 时间:2024/06/07 06:20
 
 

终于下定决心把查找和排序好好整一整,今天就弄了一个对分查找,也成为对半查找。原理十分简单,话不多说,直接上源代码。未完待续,持续更新中。。。

1、对半查找,要求输入有序序列。

// sort.cpp : 定义控制台应用程序的入口点。////--------------------------请注意对半查找要求输入序列为有序----------------------------//#include "stdafx.h"#include <iostream>using namespace std;void find_mid(int* p,int n,int low,int high){int l=low;int h=high;int mid=(l+h)/2;if (l<=h){if (*(p+mid)==n){cout<<"Found,location is"<<" "<<mid+1<<endl;}else if (*(p+mid)<n){//l=mid+1;find_mid(p,n,l+1,h);}else{//h=mid-1;find_mid(p,n,l,h-1);}}if (l>h){cout<<"The number you want does not exist"<<endl;}}int _tmain(int argc, _TCHAR* argv[]){int len;int *a;int num;cout<<"Please input the array length:"<<endl;cin>>len;a = (int*) malloc(len*sizeof(int));cout<<"Please Input the numbers:"<<endl;for (int i = 0; i < len; i++){cin>>a[i];}cout<<"please input the number you want to search:"<<endl;cin>>num;find_mid(a,num,0,len-1);return 0;}


 2.分块查找

分块查找的原理可见http://student.zjzk.cn/course_ware/data_structure/web/chazhao/chazhao9.2.3.htm

直接附上代码

#include "stdafx.h"#include <iostream>using namespace std;typedef struct index{int pos;int key;}idex;idex* findMaxLoc(int* p,int n,idex* id){int max=*p;int i=0,t=0;for(i=1;i<n;i++){if(*(p+i)>max){max=*(p+i);t=i;}}id->pos=t;id->key=max;return id;}int findParts(int* p,int n,int low,int high,int s) {int l=low;int h=high;int mid=(l+h)/2;static int cnt;if (l<=h){if (*(p+mid)==n){cout<<"Found,location is"<<" "<<mid+1<<endl;}else if (*(p+mid)>n){findParts(p,n,l,mid-s,s);}else{findParts(p,n,mid+s,h,s);}}if (l>h){cnt=(h+s)/s;cout<<"the number you are looking for is at "<<cnt<<" parts"<<endl;}return cnt;}void block_search(int* p,int n,int num){int b=3;                     //分成3块int s=n/b;                   //每块有s个元素int j;idex* id;id=(idex*) malloc(3*sizeof(idex)); for(int i=0;i<b;i++){findMaxLoc(p+i*s,s,(id+i));(id+i)->pos+=i*s;cout<<"pos is"<<(id+i)->pos<<" max is"<<(id+i)->key<<endl;}j=findParts(p,num,s-1,s*b-1,s);for (int i = j*s; i < (j+1)*s; i++){if (*(p+i)==num){cout<<"found, pos is "<<i+1<<endl;break;}else{continue;}}}int _tmain(int argc, _TCHAR* argv[]){int len;int *a;int num;cout<<"Please input the array length:"<<endl;cin>>len;a = (int*) malloc(len*sizeof(int));cout<<"Please Input the numbers:"<<endl;cout<<"tips:for block searching, the numbers are devided into 3 parts and this depends to you"<<endl;for (int i = 0; i < len; i++){cin>>a[i];}cout<<"please input the number you want to search:"<<endl;cin>>num;/*find_mid(a,num,0,len-1);*/block_search(a,len,num);return 0;}

分块查找的性能介于顺序查找和对半查找之间。

 

3. 哈希表查找

hash查找的原理随便百度就知道了,下面直接给出源码,欢迎您的批评与指正。

// find.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <iostream>using namespace std;typedef struct HashTabel{int *number;      //元素的值int length;     //指HASH表的表长}hashtabel;void InitHash(hashtabel *h,int l){h->length=l;h->number=(int*) malloc(l*sizeof(int));for (int i = 0; i < l; i++){*(h->number+i)=0;}}int hash_key(int *p,int n){int key;key=*p%n;return key;}int prime(int n){int i;                //i为不大于一个数的最大质数int j;int k;for (i = n; i > 1; i--){k=i/2;for (j = 2; j <= k; j++){if (i%j==0){break;}}if (j==k+1){return i;break;}}}void insert(hashtabel *h,int *p,int index){h->number[index]=*p;}void search_elem(hashtabel *h,int *p,int *n,int index)           //说明:p为hash表中指向下一个数的指针,*n为要插入的数值,index为下表{if (*p==0){insert(h,n,index);}else{if (*p==*n){cout<<"Found, position is "<<index<<endl;}else{search_elem(h,p+1,n,index+1);}}}void search(hashtabel *h,int *p){int max=h->length;int k;int key;int index;k=prime(max);key=hash_key(p,k);index=key;if (h->number[key]==0)          //这个位置为空就进行插入{insert(h,p,key);}else{if (h->number[key]==*p){cout<<"Found,position is "<<key<<endl;}else{index=(index+1)%6;search_elem(h,h->number+key+1,p,index);                            //不为空时则采用二次探测再散列法}}}int _tmain(int argc, _TCHAR* argv[]){int len;  int *a;  int num;  hashtabel H;cout<<"Please input the array length:"<<endl;  cin>>len;  a = (int*) malloc(len*sizeof(int));  cout<<"Please Input the numbers:"<<endl;  InitHash(&H,len);for (int i = 0; i < len; i++)  {  cin>>a[i];  search(&H,&a[i]);}  return 0;}