0
0
Data Structures Theoryknowledge~6 mins

Common complexity classes (O(1), O(n), O(log n), O(n²)) in Data Structures Theory - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine you want to know how long a task will take as the size of the input grows. Understanding common complexity classes helps predict how efficient an algorithm is and how it will perform with larger data.
Explanation
O(1) - Constant Time
An algorithm with O(1) complexity takes the same amount of time regardless of input size. It performs a fixed number of steps, like accessing a single item in a list by its position.
O(1) means the operation time does not change as input size grows.
O(n) - Linear Time
In O(n) complexity, the time grows directly in proportion to the input size. For example, checking each item in a list one by one takes longer as the list gets bigger.
O(n) means time increases linearly with input size.
O(log n) - Logarithmic Time
O(log n) complexity means the time grows slowly as input size increases, often by cutting the problem size in half each step. Searching in a sorted list by repeatedly dividing it is an example.
O(log n) means time grows slowly by reducing problem size each step.
O(n²) - Quadratic Time
With O(n²) complexity, time grows proportionally to the square of the input size. This happens when an algorithm compares every item with every other item, like in simple sorting methods.
O(n²) means time increases very quickly as input size grows.
Real World Analogy

Imagine sorting a deck of cards. If you pick one card without looking, it takes the same time no matter how many cards there are. If you look at each card once, time grows with the number of cards. If you split the deck in half repeatedly to find a card, it takes fewer steps. But if you compare every card to every other card, it takes much longer as the deck grows.

O(1) - Constant Time → Picking one card from a deck without looking at others
O(n) - Linear Time → Looking at each card one by one
O(log n) - Logarithmic Time → Splitting the deck in half repeatedly to find a card
O(n²) - Quadratic Time → Comparing every card with every other card
Diagram
Diagram
Input Size (n) →

O(1)   ──────────────■─────────────

O(log n) ─────■─────■────■────■────

O(n)   ■────■────■────■────■────■──

O(n²)  ■■■■■■■■■■■■■■■■■■■■■■■■■■■■
This diagram shows how time grows with input size for each complexity class, from constant to quadratic.
Key Facts
O(1) - Constant TimeAlgorithm time does not change with input size.
O(n) - Linear TimeAlgorithm time grows directly with input size.
O(log n) - Logarithmic TimeAlgorithm time grows slowly by halving problem size each step.
O(n²) - Quadratic TimeAlgorithm time grows with the square of input size.
Code Example
Data Structures Theory
def constant_time_example(lst):
    return lst[0]

def linear_time_example(lst):
    total = 0
    for item in lst:
        total += item
    return total

def logarithmic_time_example(lst, target):
    low, high = 0, len(lst) - 1
    while low <= high:
        mid = (low + high) // 2
        if lst[mid] == target:
            return mid
        elif lst[mid] < target:
            low = mid + 1
        else:
            high = mid - 1
    return -1

def quadratic_time_example(lst):
    pairs = []
    for i in range(len(lst)):
        for j in range(len(lst)):
            pairs.append((lst[i], lst[j]))
    return pairs

# Example usage
print(constant_time_example([10, 20, 30]))
print(linear_time_example([1, 2, 3]))
print(logarithmic_time_example([1, 2, 3, 4, 5], 4))
print(len(quadratic_time_example([1, 2, 3])))
OutputSuccess
Common Confusions
Thinking O(1) means the algorithm is always fast.
Thinking O(1) means the algorithm is always fast. O(1) means time does not grow with input size, but the fixed time could still be large depending on the operation.
Believing O(log n) is faster than O(1).
Believing O(log n) is faster than O(1). O(1) is always faster for large inputs because it does not depend on input size, while O(log n) grows slowly but still increases.
Assuming O(n²) is acceptable for large inputs.
Assuming O(n²) is acceptable for large inputs. O(n²) grows very quickly and becomes impractical for large inputs, often needing more efficient algorithms.
Summary
Common complexity classes describe how algorithm time changes as input size grows.
O(1) is constant time, O(n) is linear, O(log n) is logarithmic, and O(n²) is quadratic.
Understanding these helps choose efficient algorithms for different problems.