๐Ÿง  Time Complexity Simplified

VAIDIK DUBEYVAIDIK DUBEY
2 min read

โ“ What is Time Complexity?

Letโ€™s take a simple real-life example to demystify time complexity. Suppose you have 5 friends and you want to find out who runs the fastest. You ask each to run and record their timesโ€”the one with the lowest time is the winner.

Similarly, time complexity helps us measure how fast or slow a program performs a task. It gives an estimate of the execution time based on input size. Understanding it allows us to analyze and improve code efficiency.

๐Ÿงพ Notations of Time Complexity:

There are 3 notations to describe time complexity:

  1. ฮฉ (Omega) โ€“ Best-case: e.g., already sorted array.

  2. ฮธ (Theta) โ€“ Average-case: average over all input cases.

  3. O (Big-O) โ€“ Worst-case: e.g., reverse-sorted array.

๐Ÿ“ By default, we use Big-O unless mentioned otherwise, since it tells us the upper bound of time required.

๐Ÿ“Š Types of Time Complexities:

  • โšก Constant โ€“ O(1): Execution time stays constant regardless of input.

  • ๐Ÿ“‰ Logarithmic โ€“ O(logn): Time increases slowly as input grows.

  • ๐Ÿ“ˆ Linear โ€“ O(n): Time grows directly in proportion to input size.

  • ๐Ÿ“‰๐Ÿ“ˆ Linear Logarithmic โ€“ O(nlogn): Combination of linear and logarithmic growth.

  • ๐Ÿ” Quadratic โ€“ O(nยฒ): Time increases as the square of input size.

  • ๐Ÿ”ƒ Cubic โ€“ O(nยณ): Time grows with the cube of the input size.

  • ๐Ÿš€ Exponential โ€“ O(aโฟ): Time explodes rapidly with input size.

๐Ÿชœ Order of Time Complexities:

From fastest to slowest growth:

O(1) < O(logn) < O(n) < O(nlogn) < O(nยฒ) < O(nยณ) < O(aโฟ)

The higher in the order, the more time-consuming the program is.

โœ… Conclusion:

Understanding Time Complexity is essential for writing efficient code. It not only helps in identifying bottlenecks but also prepares you for technical interviews and real-world problem solving. Mastering this concept will help you choose the most optimal approach, saving time and computing resources. ๐Ÿš€

2
Subscribe to my newsletter

Read articles from VAIDIK DUBEY directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

VAIDIK DUBEY
VAIDIK DUBEY