Linear Search Algorithm in DSA Java - Time & Space Complexity
We want to know 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.
public static int linearSearch(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}
This code looks through each item in the array one by one to find the target value.
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 once for every element in the array, from 1 to n times.
As the array gets bigger, the number of checks grows roughly the same as the 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 grow directly with the number of items.
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 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? How would the time complexity change if you used a different search method?"
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
Binary search divides list, hash uses extra memory, linear is simple sequential check.Final Answer:
Check each element one by one until the target is found or list ends -> Option DQuick Check:
Linear search = sequential check [OK]
- Confusing linear search with binary search
- Thinking linear search requires sorted list
- Assuming linear search uses extra memory
arr of length n?Solution
Step 1: Understand array indexing in Java
Java arrays start at index 0 and go up to n-1, so loop must run from 0 to less than n.Step 2: Check loop conditions
for (int i = 0; i < n; i++) uses i = 0 to i < n, which covers all valid indices correctly.Final Answer:
for (int i = 0; i < n; i++) -> Option AQuick Check:
Array index 0 to n-1 = for loop i=0 to i<n [OK]
- Using <= n causes index out of bounds error
- Starting loop at 1 misses first element
- Confusing array length with last index
int[] arr = {4, 8, 15, 16, 23, 42};
int target = 15;
int index = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
index = i;
break;
}
}
System.out.println(index);Solution
Step 1: Loop through array to find target 15
Indices: 0=4, 1=8, 2=15. At i=2, arr[i] == 15 matches target.Step 2: Assign index and break loop
index is set to 2 and loop stops immediately.Final Answer:
2 -> Option CQuick Check:
Target 15 found at index 2 [OK]
- Not breaking loop causes last index output
- Printing target value instead of index
- Confusing index with array value
int[] arr = {1, 3, 5, 7};
int target = 5;
int index = -1;
for (int i = 1; i <= arr.length; i++) {
if (arr[i] == target) {
index = i;
break;
}
}
System.out.println(index);Solution
Step 1: Identify loop index error
Loop starts at 1 and goes to i <= arr.length, which causes index out of bounds at i = arr.length.Step 2: Correct loop to valid indices
Loop should start at 0 and run while i < arr.length to cover all valid indices safely.Final Answer:
Change loop to: for (int i = 0; i < arr.length; i++) -> Option BQuick Check:
Loop indices 0 to length-1 avoid errors [OK]
- Using <= arr.length causes runtime error
- Starting loop at 1 misses first element
- Ignoring array index bounds
arr = {2, 4, 7, 6, 8, 10}. You want to find the index of the first even number greater than 5 using linear search. Which code snippet correctly implements this?Solution
Step 1: Understand the condition to find
We want the first element that is even and greater than 5.Step 2: Check each option's condition
for (int i = 0; i < arr.length; i++) { if (arr[i] > 5 && arr[i] % 2 == 0) { System.out.println(i); break; } } checks both conditions correctly; others miss one or use wrong condition.Final Answer:
for (int i = 0; i < arr.length; i++) { if (arr[i] > 5 && arr[i] % 2 == 0) { System.out.println(i); break; } } -> Option AQuick Check:
Check both conditions with && for correct index [OK]
- Checking only one condition instead of both
- Using || instead of && causing wrong matches
- Not breaking loop after first match
Ready to prove it?
Pass the test and this pattern moves unknown
