Cautare secventiala
#include<iostream>Cautare binara - sir ordonat cresqtor
using namespace std;
void citire(int &n, int a[])
{int i;
cin>>n;
for(i=1;i<=n;i++)
cin>>a[i];
}
void afisare(int n, int a[])
{int i;
for(i=1;i<=n;i++)
cout<<a[i];
}
int cautare(int n, int a[], int x)
{int i;
for(i=1;i<=n;i++)
if(a[i]==x)
return i;
return 0;
}
int main()
{int a[100],n,x;
citire(n,a);
cout<<"x=";
cin>>x;
int rez;
rez=cautare(n,a,x);
if(rez)
cout<<x<<" Apare pe pozitia "<<rez;
else cout<<"Nu exista";
}
int binar(int a[],int x, int st, int dr)
{int m;
if(st>dr)
return -1;
m=(st+dr)/2;
if(a[m]>x)
{return binar(a,x,st,m-1);}
else if(a[m]<x) {return binar(a,x,m+1,dr);}
else return m;
}