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 does the number of steps grow when we look for an item in a list?
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;
}
}
return -1;
}
This code looks for a target value in an array by checking each element one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each element in the array to see if it matches the target.
- How many times: Up to n times, where n is the size of the array.
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 number of steps grows directly with the size of the list.
Time Complexity: O(n)
This means the time to find the item 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 time.
Understanding linear search time helps you compare it with faster methods and shows you how to 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 from start to end.Step 2: Compare with other search methods
Other methods like binary search divide the list, but linear search does not.Final Answer:
Check each element one by one until the target is found or list ends -> Option AQuick Check:
Linear search = check elements one by one [OK]
- Confusing linear search with binary search
- Thinking linear search requires sorted list
- Assuming instant search with hash tables
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 for size n.Step 2: Choose correct loop condition
The loop should run from 0 to less than n, soi < nis correct.Final Answer:
for (int i = 0; i < n; i++) -> Option AQuick Check:
Loop from 0 to n-1 = for(i=0; i<n; i++) [OK]
- Using <= n causes out-of-bounds access
- Starting loop at 1 misses first element
- Confusing array size with last index
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;
}
}
printf("%d", 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), stop loop.Step 2: Identify index where target found
Target 7 found at index 2, so index = 2.Final Answer:
2 -> Option BQuick Check:
Target 7 at arr[2] = 2 [OK]
- Printing value instead of index
- Not breaking loop and printing last index
- Confusing index with element value
int linearSearch(int arr[], int n, int target) {
for (int i = 1; i <= n; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}Solution
Step 1: Check loop indexing for array access
Array indices in C start at 0 and go to n-1, but loop starts at 1 and goes to n, causing out-of-bounds at arr[n].Step 2: Identify correct loop bounds
Loop should start at 0 and run while i < n to avoid invalid access.Final Answer:
Loop starts at 1 and goes to <= n, causing out-of-bounds access -> Option DQuick Check:
Array index out-of-bounds due to wrong loop limits [OK]
- Starting loop at 1 instead of 0
- Using <= n instead of < n in loop condition
- Returning element instead of index
arr with 10 elements where some elements repeat. You want to find the first occurrence index of the number 5 using linear search. Which approach is best to handle duplicates and return the correct index?Solution
Step 1: Understand requirement for first occurrence
We want the first index where 5 appears, so searching from start is needed.Step 2: Choose approach that stops at first match
Linear search that returns immediately on first 5 found gives correct first occurrence index.Final Answer:
Traverse from start to end, return index immediately when 5 is found -> Option CQuick Check:
Stop at first match to get first occurrence [OK]
- Counting all occurrences then returning last index
- Sorting changes original order, losing first occurrence
- Using hash map adds unnecessary complexity
Ready to prove it?
Pass the test and this pattern moves unknown
