Help with big O notation
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Big O notation is a mathematical concept used in computer science to describe the performance or complexity of an algorithm. Specifically, it characterizes how an algorithm's runtime or space requirements grow relative to the input size. Understanding Big O notation is crucial for evaluating the efficiency of algorithms, particularly in terms of time complexity and space complexity. In this article, we’ll explore the basics of Big O notation, delve into some common complexity classes, and provide examples and a comparison table for easy reference.
Basics of Big O Notation
Big O notation provides an upper bound on the running time of an algorithm, giving us an asymptotic analysis of how the runtime increases as the input grows. The notation is expressed as , where is a function that describes the growth of the algorithm relative to the input size .
Why Big O Matters
- Efficiency - It helps determine the efficiency of an algorithm.
- Scalability - It aids in understanding how algorithms perform as the scale of the input data increases.
- Comparison - It provides a way to compare the theoretical performance of different algorithms.
Common Big O Complexities
Below are some of the most common Big O complexity classes, along with their meanings and examples:
- Constant Time:
- Description: The algorithm takes a constant amount of time, regardless of the input size.
- Example: Accessing an element in an array by index.
- Description: The algorithm’s running time increases logarithmically with the input size. It is typically characteristic of algorithms that halve the problem size each step.
- Example: Binary search on a sorted array.
- Description: The running time increases linearly with the size of the input.
- Example: Finding the maximum element in an unsorted list.
- Description: The algorithm's time complexity grows n log n times with the input size and is typical of efficient sorting algorithms.
- Example: Merge sort or quicksort.
- Description: The running time is proportional to the square of the input size, often seen in algorithms with nested loops.
- Example: Bubble sort.
- Description: The running time increases cubically with the input size.
- Example: The Floyd-Warshall algorithm for finding shortest paths in a weighted graph.
- Description: The runtime doubles with each additional element, characteristic of brute-force search algorithms.
- Example: Solving the traveling salesman problem via brute force.
- Description: The factorial growth of the running time is typical in algorithms generating all permutations of a set.
- Example: Generating permutations recursively.
- Worst Case vs. Average Case: Big O typically considers the worst-case scenario. However, understanding average-case complexity can also be important, especially for practical applications.
- Space Complexity: In addition to time complexity, Big O notation can also express the space complexity, describing how much memory an algorithm uses as the input size increases.
- Constants and Lower Order Terms: Big O disregards constants and lower-order terms because, as the input size grows infinitely large, these factors have a diminishing effect on the growth rate.
Related reading
- Helper library for distributed algorithms programming?
- Hexagonal Grids, how do you find which hexagon a point is in?
- Hidden Markov Models with C
- Hierarchical clustering of 1 million objects
- High GPU Memory-Usage but zero volatile gpu-util
- High memory consumption on kafka consumer
- Highest Valued Palindrome
- Hilbert sort by divide and conquer algorithm?

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 courseTrack 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.