-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearSearch.cpp
More file actions
35 lines (35 loc) · 802 Bytes
/
LinearSearch.cpp
File metadata and controls
35 lines (35 loc) · 802 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
33
34
35
#include <iostream>
#include <vector>
using namespace std;
int search(vector<int> arr, int n, int key)
{
for (int i = 0; i < n; i++)
{
if (arr[i] == key)
return i;
}
return -1;
}
int main()
{
vector<int> arr;
int n, a, i, key;
cout << "Enter the size of the array: ";
cin >> n;
cout << "Enter the elements of the array: ";
for (i = 0; i < n; i++)
{
cin >> a;
arr.push_back(a);
}
cout << "Enter the element to be searched: ";
cin >> key;
int ans = search(arr, n, key);
if (ans == -1)
cout << "The element is not present in the array." << endl;
else
cout << "The element is present at the position " << ans + 1 << endl;
return 0;
}
// Time Complexity=O(n)
// Space Complexity=O(1)