CSC325 ยท 2082 ยท 5 marks
State the time and space complexity for sequential search. Write the rules for master theorem for finding asymptotic bounds.
State the time and space complexity for sequential search. Write the rules for master theorem for finding asymptotic bounds. [5]
Quick answer
Sequential search (also called linear search) compares the search element with each element in the list one by one from the beginning.
Sequential Search: Time & Space Complexity + Master Theorem Rules
Part 1: Time and Space Complexity of Sequential Search
Time Complexity
| Case | Condition | Complexity |
|---|---|---|
| Best Case | Element found at the first position | O(1) |
| Average Case | Element found somewhere in the middle | O(n) |
| Worst Case | Element found at last position or not found at all | O(n) |
Since the loop executes at most n times, the overall time complexity is O(n).
Space Complexity
- Sequential search only requires a fixed number of variables (search element, loop counter, etc.)
- No extra data structure is used
- Therefore, space complexity = O(1) (constant space)
Part 2: Master Theorem Rules for Asymptotic Bounds
The Master Theorem is used to find the time complexity of divide-and-conquer recursive algorithms whose recurrence relation is of the form:
$$T(n) = aT\left(\frac{n}{b}\right) + f(n)$$
Where:
a >= 1: number of subproblemsb > 1: factor by which input size is reducedf(n): cost of work done outside the recursive calls
Let p = log_b(a)
Rule 1: f(n) is polynomially smaller than n^p
If:
$$f(n) = O(n^{p - \epsilon}) \quad \text{for some } \epsilon > 0$$
Then:
$$T(n) = \Theta(n^p) = \Theta(n^{\log_b a})$$
The recursive work dominates.
Rule 2: f(n) is asymptotically equal to n^p
If:
$$f(n) = \Theta(n^p) = \Theta(n^{\log_b a})$$
Then:
$$T(n) = \Theta(n^p \log n) = \Theta(n^{\log_b a} \cdot \log n)$$
Both the recursive work and the combining work are equal, so a log factor is added.
Rule 3: f(n) is polynomially larger than n^p
If:
$$f(n) = \Omega(n^{p + \epsilon}) \quad \text{for some } \epsilon > 0$$
AND the regularity condition holds:
$$a \cdot f\left(\frac{n}{b}\right) \leq c \cdot f(n) \quad \text{for some } c < 1$$
Then:
$$T(n) = \Theta(f(n))$$
The combining work dominates.
Summary Table
| Condition | Result |
|---|---|
| f(n) < n^(log_b a) polynomially | T(n) = Theta(n^(log_b a)) |
| f(n) = Theta(n^(log_b a)) | T(n) = Theta(n^(log_b a) * log n) |
| f(n) > n^(log_b a) polynomially | T(n) = Theta(f(n)) |
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.