-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
72 lines (66 loc) · 1.2 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
void Binary(int a[],int l,int h ,int key)
{
int mid;
mid=(l+h)/2;
if(h<l)
{
cout<<"\nMatch Not Found\n";
return;
}
if(a[mid]<key)
{
return(Binary(a,mid+1,h,key));
}else if(a[mid]>key)
{
return(Binary(a,l,mid-1,key));
}else
{
cout<<"\nMatch Found at position \n"<<mid;
return;
}
}
void linear(int a[],int key,int c,int l)
{
if(c==l)
{
cout<<"\nMatch Not Found\n";
return;
}
if(a[c]!=key)
{
return(linear(a,key,c+1,l));
}else
{
cout<<"\nMatch Found At Position:\n"<<c;
return;
}
}
int main()
{
int n;
cout<<"Enter Total No of Elements ";
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
a[i]=rand()%5000;
}
cout<<"Given Array\n";
for(int i =0;i<n;i++)
{
cout<<a[i]<<"\t";
}
int key;
cout<<"\nEnter Key :\n";
cin>>key;
cout<<"LINEAR SEARCH\n";
linear(a,key,0,n);
sort(a,a+n);
cout<<"BINARY SEARCH\n";
Binary(a,0,n,key);
}