algorithm
big-o notation
code complexity
computational efficiency
time complexity

Big-O complexity of a piece of code

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Understanding the Big-O complexity of a piece of code is crucial for analyzing its efficiency, especially as the size of the input grows. This article will delve into the concept, providing technical explanations and examples to elucidate the topic.

Introduction to Big-O Notation

Big-O notation is a mathematical representation that describes the upper bound of an algorithm's running time or space requirements in terms of the input size. It characterizes algorithms by their growth rate, facilitating a high-level understanding of performance efficiency. The primary focus in Big-O analysis is on the input size affecting the performance as it approaches infinity.

Big-O Complexity Classes

Several standard complexity classes are typically used to describe algorithms:

  1. Constant Time - O(1)O(1):
    • The running time is unaffected by the size of the input.
    • Example: Accessing an element in an array by index.
  2. Logarithmic Time - O(logn)O(\log n):
    • The running time increases logarithmically with the input size.
    • Example: Binary search on a sorted array.
  3. Linear Time - O(n)O(n):
    • The running time grows linearly with the input size.
    • Example: Iterating through an array of `n` elements.
  4. Linear Logarithmic Time - O(nlogn)O(n \log n):
    • Common in sorting algorithms like merge sort or quicksort.
    • Example: Sorting elements using an efficient sorting algorithm.
  5. Quadratic Time - O(n2)O(n^2):
    • Running time is proportional to the square of the input size.
    • Example: A simple two-dimensional nested loop.
  6. Cubic Time - O(n3)O(n^3):
    • Running time scales cubically with input size.
    • Example: Triple nested loops over `n`.
  7. Exponential Time - O(2n)O(2^n):
    • Grows exponentially as input increases.
    • Example: Solving the traveling salesman problem via brute force.
  8. Factorial Time - O(n!)O(n!):
    • Infeasible for large `n` due to rapid growth.
    • Example: Permutations of a set.

Analyzing a Piece of Code

Let's analyze the Big-O complexity of a simple code snippet:

  • Setting `sum = 0` is a constant time operation, O(1)O(1).
  • The `for` loop executes `n` times (where `n` is the length of `arr`), having a time complexity of O(n)O(n).
  • The function's overall complexity is O(n+1)O(n + 1). However, Big-O notation focuses on the term with the highest growth rate, ignoring lower-order terms and coefficients. Thus, the complexity simplifies to O(n)O(n).

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.