forked from IOSD/Algo
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLinear_Search.cpp
35 lines (34 loc) · 1.07 KB
/
Linear_Search.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
//LINEAR SEARCH ALGORITHM
#include<iostream>
using namespace std;
int linsearch(int a[],int b,int c) //FUNCTION FOR LINEAR SEARCH TAKING ARRAY , SIZE, NO TO FIND AS ARGUMENTS
{
for(int i=0;i<b;i++)
{ if(a[i]==c) //CHECKING OF CONDITION TO FIND VARIABLE
return (i+1);
}
return -1;
}
//TIME COMPLEXITY : O(n)
//SPACE COMPLEXITY :O(1)
int main()
{
int *arr,n,k; //INIT. OF ARRAY
cout<<"Enter no of Entries:";
cin>>n; //INPUT OF NO OF ENTRIES IN ARRAY
arr=new int[n]; //DYNAMIC INITIALISATION OF ARRAY
for(int i=0;i<n;i++)
{
cin>>arr[i]; //INPUT OF ARRAY
}
cout<<"ENTER NO TO BE SEARCHED";
cin>>k; //NO TO BE SEARCHED
k=linsearch(arr,n,k); //CALLING OF FUCTION LINSEARCH
if(k==-1) // IF RETURNS -1 -> NOT FOUND ELSE PRINTING POSITION
cout<<"NOT FOUND";
else
cout<<"POSITON : "<<k<<endl;
system("pause");
delete arr;
return 0;
}