Experiment 3 search algorithm

problem:

Two retrieval algorithms: Find a sorted array T [1..n] of x, if x is T, the output T x is the subscript j; if x is not T, the output of j = 0.

Resolution:

① directly through the array to find x.

② binary search.

Design (core code):

① traversal

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

② half

 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\n",mid);
14             return ;
15         }
16     }
17     printf("0\n");
18     return ;
19 }

analysis:

① through the array, the complexity of O (n).

② binary search, the complexity of O (logn).

Source:

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("*********\n")
 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\n",i);
 75             return ;
 76         }
 77     }
 78     printf("0\n");
 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\n",mid);
 95             return ;
 96         }
 97     }
 98     printf("0\n");
 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

 

Guess you like

Origin www.cnblogs.com/zhang-Kelly/p/12496982.html