How to Calculate Time Complexity of an Algorithm?

Identify the basic operation

Determine the input size variable

Count how many times the basic operation executes

Express the count as a function of input size

Consider best-case, average-case, and worst-case scenarios

Analyze loops one at a time

Analyze nested loops by multiplying iteration counts

Analyze sequential statements by adding their costs

Analyze conditional branches by taking the dominant branch cost

Analyze recursive calls using recurrence relations

Solve recurrences using substitution, recursion tree, or master theorem

Ignore constant factors

Ignore lower-order terms

Keep the dominant term

Express the result using Big-O notation

Verify the result with sample input sizes

Suggested for You

Trending Today