Linear Search
in C++
Learn to traverse arrays and vectors element by element to find a target value: the simplest and most fundamental search algorithm.
What is Searching?
Searching is the process of locating a specific element within a data structure. Given an array of \( n \) elements, we want to determine whether a target value exists and, if so, where it is.
Meet Linear Search
Linear search (also called sequential search) is the most straightforward approach: start at the beginning and check each element one by one until you either find the target or exhaust the entire collection.
Why Linear Search Matters
The Algorithm Formally
Time Complexity
Linear vs Binary Search
| Linear Search | Binary Search | |
|---|---|---|
| Time Complexity | \( O(n) \) | \( O(\log n) \) |
| Requires Sorting? | No | Yes |
| Data Structure | Array, vector, list | Sorted array / vector |
| Implementation | Very simple | Moderate |
| Best For | Small or unsorted data | Large sorted datasets |
How to Implement It
arr[i] with the target using ==.Code: Array Version
int linearSearch(int arr[], int n, int target) { for (int i = 0; i < n; i++) { if (arr[i] == target) { return i; // found at index i } } return -1; // not found }
Code: Vector Version
int linearSearch(const vector<int>& vec, int target) { for (int i = 0; i < (int)vec.size(); i++) { if (vec[i] == target) { return i; } } return -1; }
Example 1: Target Found
Array: {4, 2, 7, 1, 9, 3}, target = 9
Example 2: Target Not Found
Array: {4, 2, 7, 1, 9, 3}, target = 6
Complete Program
#include <iostream> #include <vector> using namespace std; int linearSearch(const vector<int>& v, int target) { for (int i = 0; i < (int)v.size(); i++) { if (v[i] == target) return i; } return -1; } int main() { vector<int> nums = {10, 23, 45, 70, 11, 15}; int result = linearSearch(nums, 70); if (result != -1) cout << "Found at index " << result; else cout << "Not found"; return 0; }
Searching for Strings
Linear search works on any data type that supports ==. Here is a version for strings:
int findName(const vector<string>& names, const string& target) { for (int i = 0; i < (int)names.size(); i++) { if (names[i] == target) return i; } return -1; }
Common Mistakes
true/false when the question asks for the index, or vice versa.Summary at a Glance
| Property | Detail |
|---|---|
| Algorithm Type | Sequential / Brute-Force |
| Best Case | \( O(1) \) |
| Worst Case | \( O(n) \) |
| Space Complexity | \( O(1) \) (no extra memory) |
| Requires Sorting? | No |
| Works With | Arrays, vectors, lists, strings |
| "Not Found" Return | \( -1 \) |
Question 1 of 5
What is the worst-case time complexity of linear search on an array of \( n \) elements?
Question 2 of 5
Given arr = {5, 12, 8, 3, 20} and target = 8, what does linearSearch(arr, 5, 8) return?
Question 3 of 5
What value should a linear search function return when the target is not found in the array?
Question 4 of 5
A student writes the following code:
int linearSearch(int arr[], int n, int target) { for (int i = 0; i < n; i++) { if (arr[i] == target) return i; else return -1; } }
What is wrong with this implementation?
Question 5 of 5
Which of the following is NOT an advantage of linear search over binary search?