zoukankan      html  css  js  c++  java
  • 实验3检索算法

    问题:

    两种检索算法:在一个排好序的数组T[1..n]中查找x,如果x在T中,输出x在T的下标j;如果x不在T中,输出j=0.

    解析:

    ①直接遍历数组,查找x。

    ②二分查找。

    设计(核心代码):

    ①遍历

     1 void solve1(int x)
     2 {
     3     for(int i=1;i<=n;++i)
     4     {
     5         if(a[i]==x)
     6         {
     7             printf("%d
    ",i);
     8             return ;
     9         }
    10     }
    11     printf("0
    ");
    12     return ;
    13 }

    ②二分

     1 void solve2(int x)
     2 {
     3     int l=1,r=n,mid;
     4     while(l<=r)
     5     {
     6         mid=(l+r)>>1;
     7         if(a[mid]>x)
     8             r=mid-1;
     9         else if(a[mid]<x)
    10             l=mid+1;
    11         else
    12         {
    13             printf("%d
    ",mid);
    14             return ;
    15         }
    16     }
    17     printf("0
    ");
    18     return ;
    19 }

    分析:

    ①遍历数组,复杂度O(n).

    ②二分查找,复杂度O(logn).

    源码:

    https://github.com/Big-Kelly/Algorithm

      1 #include<bits/stdc++.h>
      2 #include <set>
      3 #include <map>
      4 #include <stack>
      5 #include <cmath>
      6 #include <queue>
      7 #include <cstdio>
      8 #include <string>
      9 #include <vector>
     10 #include <cstring>
     11 #include <iostream>
     12 #include <algorithm>
     13 
     14 #define ll long long
     15 #define pll pair<ll,ll>
     16 #define pii pair<int,int>
     17 #define bug printf("*********
    ")
     18 #define FIN freopen("input.txt","r",stdin);
     19 #define FON freopen("output.txt","w+",stdout);
     20 #define IO ios::sync_with_stdio(false),cin.tie(0)
     21 #define ls root<<1
     22 #define rs root<<1|1
     23 #define Q(a) cout<<a<<endl
     24 
     25 using namespace std;
     26 const int inf = 2e9 + 7;
     27 const ll Inf = 1e18 + 7;
     28 const int maxn = 1e6 + 5;
     29 const int mod = 1e9 + 7;
     30 
     31 ll gcd(ll a, ll b)
     32 {
     33     return b ? gcd(b, a % b) : a;
     34 }
     35 
     36 ll lcm(ll a, ll b)
     37 {
     38     return a / gcd(a, b) * b;
     39 }
     40 
     41 ll read()
     42 {
     43     ll p = 0, sum = 0;
     44     char ch;
     45     ch = getchar();
     46     while (1)
     47     {
     48         if (ch == '-' || (ch >= '0' && ch <= '9'))
     49             break;
     50         ch = getchar();
     51     }
     52 
     53     if (ch == '-')
     54     {
     55         p = 1;
     56         ch = getchar();
     57     }
     58     while (ch >= '0' && ch <= '9')
     59     {
     60         sum = sum * 10 + ch - '0';
     61         ch = getchar();
     62     }
     63     return p ? -sum : sum;
     64 }
     65 
     66 int a[maxn],n;
     67 
     68 void solve1(int x)
     69 {
     70     for(int i=1;i<=n;++i)
     71     {
     72         if(a[i]==x)
     73         {
     74             printf("%d
    ",i);
     75             return ;
     76         }
     77     }
     78     printf("0
    ");
     79     return ;
     80 }
     81 
     82 void solve2(int x)
     83 {
     84     int l=1,r=n,mid;
     85     while(l<=r)
     86     {
     87         mid=(l+r)>>1;
     88         if(a[mid]>x)
     89             r=mid-1;
     90         else if(a[mid]<x)
     91             l=mid+1;
     92         else
     93         {
     94             printf("%d
    ",mid);
     95             return ;
     96         }
     97     }
     98     printf("0
    ");
     99     return ;
    100 }
    101 
    102 int main()
    103 {
    104     scanf("%d",&n);
    105     for(int i=1;i<=n;++i)
    106         scanf("%d",&a[i]);
    107     sort(a+1,a+1+n);
    108     int q;
    109     scanf("%d",&q);
    110     while(q--)
    111     {
    112         int x;
    113         scanf("%d",&x);
    114         solve1(x);
    115         solve2(x);
    116     }
    117 }
    View Code

     

  • 相关阅读:
    POJ 2996 Help Me with the Game (模拟)
    PCL系列——怎样逐渐地配准一对点云
    sublime text3同时编辑多行
    博客搬家
    将博客搬至CSDN
    centos7用xshell可以连接, xftp连接失败!(墙裂推荐)
    重启ssh服务出现Redirecting to /bin/systemctl restart sshd.service
    重装wordpress
    ubuntu 16.04 启用root用户方法
    Ubuntu创建新用户并增加管理员权限(授权有问题)
  • 原文地址:https://www.cnblogs.com/zhang-Kelly/p/12496982.html
Copyright © 2011-2022 走看看