- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinear-Search.cpp
More file actions
Latest commit
32 lines (27 loc) · 553 Bytes
/
Copy pathLinear-Search.cpp
File metadata and controls
32 lines (27 loc) · 553 Bytes
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
#include<iostream>
#include<vector>
#include<algorithm>
usingnamespacestd;
intlinearSearch(vector<int> arr, int n, int key){
for(int i= 0; i<n; i++){
if(arr.at(i)==key){
return i;
}
}
return -1;
}
intmain()
{
vector <int> arr;
int n; // lenght of array
int ele; // for adding element to the array
cin>>n;
for(int i = 0; i<n; i++){
cin>>ele;
arr.push_back(ele);
}
int key;
cin>>key;
cout<<linearSearch(arr, n, key)<<endl;
return0;
}