forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearSearch.cpp
More file actions
36 lines (25 loc) · 693 Bytes
/
LinearSearch.cpp
File metadata and controls
36 lines (25 loc) · 693 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
36
#include <iostream>
#include <vector>
using namespace std;
int linear_search(vector<int> &nums, int target) {
for (size_t i = 0; i < nums.size(); i++) {
if(nums[i] == target)
return i;
}
return -1;
}
int main() {
vector<int> nums = {1, 2, 3, 4, 5, 27, -1, 12, 999};
int target;
cout << "Enter the number you would like to search in the vector: ";
cin >> target;
cout << "\n";
int pos = linear_search(nums, target);
if(pos > -1) {
cout << "Number found in the vector in the position: " << pos << endl;
}
else {
cout << "Number not found in the vector." << endl;
}
return 0;
}