Big O Notation
Big O Notation is a mathematical tool for measuring algorithm efficiency. It describes how execution time or memory usage grows with input size, using categories such as O(1), O(n), and O(log n).
Big O Notation is a mathematical tool for measuring algorithm efficiency. It describes how execution time or memory usage grows with input size, using categories such as O(1), O(n), and O(log n).
Big O Notation is a mathematical concept used in computer science to describe the efficiency of algorithms. It expresses the relationship between the input size of a problem and the number of operations required, focusing on how performance scales rather than exact execution time.
Big O provides a high-level way of analyzing algorithm complexity, helping developers compare different approaches and choose efficient solutions.
O(1) – Constant time
Execution time does not depend on input size.
Example: Accessing an element in an array by index.
O(log n) – Logarithmic time
Input size is reduced at each step.
Example: Binary search in a sorted list.
O(n) – Linear time
Execution grows proportionally with input size.
Example: Iterating through all elements in a list.
O(n log n) – Log-linear time
Common in efficient sorting algorithms.
Example: Merge Sort, Quick Sort (average case).
O(n²) – Quadratic time
Performance decreases significantly with input growth.
Example: Nested loops, Bubble Sort.
O(2^n) – Exponential time
Becomes infeasible even for moderate inputs.
Example: Brute-force solution to the Traveling Salesman Problem.
O(n!) – Factorial time
Extremely inefficient, grows faster than exponential.
Example: Generating all permutations of a set.
# O(n): Linear time
def find_max(arr):
max_value = arr[0]
for num in arr:
if num > max_value:
max_value = num
return max_value
This function checks every element in the list, so its runtime grows linearly with input size.
Big O is also used to describe memory usage:
Big O Notation is a cornerstone of algorithm analysis in computer science. By describing how performance and memory usage scale, it guides developers in selecting efficient solutions for problems of any size.