Linear Search Algorithm in DSA C++ - Time & Space Complexity
We want to understand how the time taken by linear search changes as the list size grows.
How many steps does it take to find an item or decide it's not there?
Analyze the time complexity of the following code snippet.
int linearSearch(int arr[], int n, int target) {
for (int i = 0; i < n; i++) {
if (arr[i] == target) {
return i; // found target
}
}
return -1; // target not found
}
This code looks through each element one by one to find the target value.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each element in the array one by one.
- How many times: Up to n times, where n is the number of elements.
As the list gets bigger, the number of checks grows roughly the same as the list size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The steps increase directly with the number of items.
Time Complexity: O(n)
This means the time to find the target grows in a straight line with the list size.
[X] Wrong: "Linear search always takes the same time no matter the list size."
[OK] Correct: The search time depends on where the target is or if it is missing, so bigger lists usually take more steps.
Understanding linear search time helps you explain why more efficient methods exist and shows you can analyze simple algorithms clearly.
"What if the array was sorted and we used binary search instead? How would the time complexity change?"
Practice
Linear Search Algorithm?Solution
Step 1: Understand linear search process
Linear search checks each element in order, starting from the first, to find the target.Step 2: Compare with other search methods
Other methods like binary search divide the list, but linear search simply checks one by one.Final Answer:
Check each element one by one until the target is found or list ends -> Option BQuick Check:
Linear search = check elements one by one [OK]
- Confusing linear search with binary search
- Thinking linear search requires sorted data
- Assuming linear search is faster than binary search always
arr of size n?Solution
Step 1: Understand array indexing in C++
Arrays in C++ start at index 0 and go up to n-1, so loop should run from 0 to less than n.Step 2: Check loop conditions
Loop withi < nensures last index accessed is n-1, which is valid.Final Answer:
for (int i = 0; i < n; i++) -> Option CQuick Check:
Loop from 0 to n-1 = for (int i = 0; i < n; i++) [OK]
- Using <= n causes out-of-bound access
- Starting loop at 1 misses first element
- Using wrong loop condition causing runtime errors
int arr[] = {4, 2, 7, 1, 9};
int n = 5;
int target = 7;
int index = -1;
for (int i = 0; i < n; i++) {
if (arr[i] == target) {
index = i;
break;
}
}
std::cout << index;Solution
Step 1: Trace the loop to find target 7
Check elements: arr[0]=4 (no), arr[1]=2 (no), arr[2]=7 (yes), so index = 2 and break.Step 2: Output the index
After break, index is 2, so output is 2.Final Answer:
2 -> Option AQuick Check:
Target found at index 2 [OK]
- Forgetting to break loop after finding target
- Printing target value instead of index
- Confusing index with position (1-based)
int arr[] = {5, 3, 8, 6};
int n = 4;
int target = 6;
int index = -1;
for (int i = 1; i <= n; i++) {
if (arr[i] == target) {
index = i;
break;
}
}
std::cout << index;Solution
Step 1: Check loop start index
Loop starts at 1, so arr[0] is never checked, missing first element.Step 2: Check loop end condition
Loop runs while i <= n, so i can be 4, but arr[4] is out of bounds (valid indices 0-3).Final Answer:
Both A and B are errors -> Option AQuick Check:
Loop must start at 0 and run i < n [OK]
- Starting loop at 1 misses first element
- Using <= n causes out-of-bound access
- Ignoring array index limits
arr = {3, 5, 2, 9, 5, 2}. You want to find the first occurrence index of the number 5 using linear search. What will be the output and why?Solution
Step 1: Understand linear search behavior with duplicates
Linear search returns the first index where target is found, ignoring later duplicates.Step 2: Find first occurrence of 5 in array
arr[0]=3 (no), arr[1]=5 (yes), so index = 1 is returned immediately.Final Answer:
1, because linear search returns the first matching index -> Option DQuick Check:
First occurrence index = 1 [OK]
- Returning last occurrence instead of first
- Assuming duplicates cause errors
- Returning -1 incorrectly when target exists
Ready to prove it?
Pass the test and this pattern moves unknown
