Time Complexity of an algorithm

Time Complexity is a method to understand how much time an algorithm will take when it receives a large input size N.

  • It does not measure time in seconds.
  • It only counts the number of fundamental operations (basic steps) performed by the algorithm based on input size N.
  • Fundamental Operations include:
    • Assignment (assigning value)
    • Comparison (checking or comparing)
    • Arithmetic operations (addition, subtraction, multiplication)

Why Is It Important?

Time complexity tells how efficient an algorithm is. When the input size N becomes very large, it helps us observe how fast the algorithm's execution time increases.


How Do We Denote Time Complexity? (Asymptotic Notations)

Time complexity is expressed using 3 popular notations, among which Big O is used the most.

1. Big O Notation (O): Upper Bound

  • Also known as Worst-Case Time Complexity.
  • It tells how much time an algorithm can take in the worst-case scenario.
  • It guarantees that the algorithm’s time will not exceed this limit.
  • Example: If complexity is O(N²), it means time will not grow faster than .

2. Omega Notation (Ω): Lower Bound

  • Also known as the Best-Case Time Complexity.
  • It tells the minimum time an algorithm may take.

3. Theta Notation (Θ): Tight Bound

  • It represents the actual time, which lies between the best and worst cases.
  • It gives the most accurate representation.

Common Time Complexities

In algorithm design, some common time complexities are used very frequently.

Notation Name Growth with N Example
O(1) Constant Time Stays the same (no effect of input size) Accessing array element by index
O(log N) Logarithmic Time Grows very slowly Binary Search
O(N) Linear Time Grows directly with input Printing an array
O(N log N) Linearithmic Time Grows faster Merge Sort, Heap Sort
O(N²) Quadratic Time Grows very rapidly Bubble Sort, Selection Sort
O(2ⁿ) Exponential Time Extremely fast growth (avoid if possible) TSP (Brute Force)

Example: O(N) vs O(N²)

Suppose N = 1000:

  • O(N): Algorithm performs ~1000 operations (fast).
  • O(N²): Algorithm performs ~1,000,000 (1 million) operations (slow).

Comments

Popular posts from this blog

Data Abstraction

Data Structure Ka Parichay Aur Prakar (Introduction and Types of Data Structure)

Data Abstraction