#include <iostream>
#include <vector>
int linearSearch(const std::vector<int>& arr, int target) {
for (int i = 0; i < (int)arr.size(); i++) {
if (arr[i] == target) {
return i; // found target, return index
}
}
return -1; // target not found
}
int main() {
std::vector<int> arr = {3, 7, 1, 9, 5};
int target = 9;
int index = linearSearch(arr, target);
if (index != -1) {
std::cout << "Found at index " << index << std::endl;
} else {
std::cout << "Not found" << std::endl;
}
return 0;
}for (int i = 0; i < (int)arr.size(); i++) {
iterate over each element in the array
check if current element matches the target
return i; // found target, return index
stop search and return index when target found
return -1; // target not found
return -1 if target is not in the array