c++ 基本的查找算法

顺序查找-比较简单,循环比较即可
[cpp]  view plain  copy
  1. /*Find*/  
  2. //顺序查找算法,循环比较即可  
  3. #include <iostream>  
  4. #include <time.h>  
  5. #define N 15  
  6. using namespace std;  
  7. int Search(int a[],int n,int key)  
  8. {  
  9.     int i,f=-1;  
  10.     for(i=0;i<n;i++)  
  11.     {  
  12.         if(key==a[i])  
  13.         {  
  14.             f=i;  
  15.             break;  
  16.         }  
  17.     }  
  18.     return f;  
  19. };  
  20.   
  21. int main() {  
  22.     cout<<"hello"<<endl;  
  23.     int shuzi[N];  
  24.     srand(time(NULL));  
  25.     for(int i=0;i<N;i++)  
  26.     {  
  27.         shuzi[i]=rand()%100;  
  28.     }  
  29.     cout<<"************show the data*************"<<endl;  
  30.     for(int i=0;i<N;i++)  
  31.     {  
  32.         cout<<shuzi[i]<<" ";  
  33.     }  
  34.     cout<<endl;  
  35.     cout<<"input the number you want to find"<<endl;  
  36.     int temp;  
  37.     cin>>temp;  
  38.     int pos = Search(shuzi,N,temp);  
  39.     if(pos<0)  
  40.     {  
  41.         cout<<"can't find the data!"<<endl;  
  42.     }  
  43.     else  
  44.     {  
  45.         cout<<"the data is located in SHUZI at "<<pos+1<<endl;  
  46.     }  
  47.     return 0;  
  48. }  

折半查找-适用于有序的数据的查询

[cpp]  view plain  copy
  1. /*折半查找*/  
  2. #include <iostream>  
  3. using namespace std;  
  4. #define N 15  
  5. int Search(int a[],int n,int key)  
  6. {  
  7.     int mid,low,high;  
  8.     low = 0;  
  9.     high = n-1;  
  10.     while(low<=high)  
  11.     {  
  12.         mid = (low+high)/2;  
  13.         if(a[mid]==key)  
  14.             return mid;  
  15.         else  
  16.         {  
  17.             if(a[mid]>key)  
  18.                 high = mid-1;  
  19.             if(a[mid]<key)  
  20.                 low = mid+1;  
  21.         }  
  22.     }  
  23.     return -1;  
  24. }  
  25. int main()  
  26. {  
  27.     int shuzi[N]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};  
  28.     /*srand(time(NULL)); 
  29.     for(int i=0;i<N;i++) 
  30.     { 
  31.         shuzi[i]=rand()%100; 
  32.     }*/  
  33.     cout<<"************show the data*************"<<endl;  
  34.     for(int i=0;i<N;i++)  
  35.     {  
  36.         cout<<shuzi[i]<<" ";  
  37.     }  
  38.     cout<<endl;  
  39.     cout<<"input the number you want to find"<<endl;  
  40.     int temp;  
  41.     cin>>temp;  
  42.     int pos = Search(shuzi,N,temp);  
  43.     if(pos<0)  
  44.     {  
  45.         cout<<"can't find the data!"<<endl;  
  46.     }  
  47.     else  
  48.     {  
  49.         cout<<"the data is located in SHUZI at "<<pos+1<<endl;  
  50.     }  
  51.   
  52.     return 0;  
  53. }  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值