#include <iostream.h>
#include <conio.h>
#include <dos.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
const n=100;
int a[n];
int search (int x)
{
int i;
for (i=0;i<n;i++)
if (a[i]==x)
return i;
return -1;
}
//-----------------------------------
//binary
int bs (int a[],int b,int x)
{
int low=0,high,mid;
high=b-1;
while (low<=high)
{
mid=(low+high)/2;
if (a[mid]==x)
return mid;
else if (a[mid]>x)
high=mid-1;
else
low=mid+1;
}
return -1;
}
//------------------------------------
int input()
{
randomize();
for(int i=0;i<100;i++)
a[i]=rand() % 100;
return 0;
}
//-----------------------------------
void main()
{
struct time t1,t2;
struct time t3,t4;
int y,k;
int d,i,q;
cout<<"\n1)tartibi search \n2)bainary search\n3)exit \n \n";
cout<<"\n---------------------------------------------------------------------------\n";
while (q !=3)
{
printf(" \n select...");
cin >>q;
if (q==1)
{
printf ("tedade anasore araye:");
scanf("%d",&d);
for(i=0;i<d;i++)
{
printf("enter num :");
scanf ("%d",&a[i]);
}
printf("enter num for finde :");
scanf ("%d",&y);
gettime(&t1);
k=search(y);
delay(2000);
gettime(&t2);
if (k!=-1)
printf("\nnum found\n");
else
printf("\nnot found\n");
printf("\ntime: %02d:%02d\n", t2.ti_sec-t1.ti_sec, t2.ti_hund-t1.ti_hund);
}
else if (q==2)
{
int k,x,b,i;
int *a;
printf ("tedade anasore araye:");
scanf("%d",&b);
a=new int [b];
struct time t1,t2;
for(i=0;i<b;i++)
{
printf("enter num :");
scanf ("%d",&a[i]);
}
printf("enter num for finde :");
scanf ("%d",&x);
gettime(&t3);
k=bs(a,b,x);
delay(2000);
gettime(&t4);
if (k!=-1)
printf("\nnum found\n");
else
printf("\nnot found\n");
printf("\ntime: %02d:%02d\n", t4.ti_sec-t3.ti_sec, t4.ti_hund-t3.ti_hund);
}
} //while
}