2082 9

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]

Design and Analysis of AlgorithmsChapter 2: Iterative AlgorithmsSearching Algorithms

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


Time Complexity

CaseConditionComplexity
Best CaseElement found at the first positionO(1)
Average CaseElement found somewhere in the middleO(n)
Worst CaseElement found at last position or not found at allO(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 subproblems
  • b > 1 : factor by which input size is reduced
  • f(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

ConditionResult
f(n) < n^(log_b a) polynomiallyT(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) polynomiallyT(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.