Big O notation
Big O notation tells you how quickly an algorithm’s running time or memory use grows as its input gets bigger. It captures the worst-case growth rate instead of an exact step count, ignoring constant factors and lower-order terms, so you can compare how algorithms scale regardless of hardware or language.
A function written inside O(...) expresses an upper bound on growth. Common classes run from O(1) constant and O(log n) logarithmic, through O(n) linear and O(n log n) linearithmic, up to O(n^2) quadratic and O(2^n) exponential. The variable, n, is the size of the input, such as the number of elements in an array.
These classes line up from cheapest to most expensive as the input grows:
The notation underlies both time complexity and space complexity analysis, and it appears throughout the study of algorithms.
Related Resources
Tutorial
Sorting Algorithms in Python
In this tutorial, you'll learn all about five different sorting algorithms in Python from both a theoretical and a practical standpoint. You'll also learn several related and important concepts, including Big O notation and recursion.
For additional information on related topics, take a look at the following resources:
- Common Python Data Structures (Guide) (Tutorial)
- How to Do a Binary Search in Python (Tutorial)
- Recursion in Python: An Introduction (Tutorial)
- Introduction to Sorting Algorithms in Python (Course)
- Build a Hash Table in Python With TDD (Tutorial)
- Profiling in Python: How to Find Performance Bottlenecks (Tutorial)
- Profiling Performance in Python (Course)
- Pure Python vs NumPy vs TensorFlow Performance Comparison (Tutorial)
- Stacks and Queues: Selecting the Ideal Data Structure (Course)
- Records and Sets: Selecting the Ideal Data Structure (Course)
- Dictionaries and Arrays: Selecting the Ideal Data Structure (Course)
- Creating a Binary Search in Python (Course)
- Recursion in Python (Course)
- Recursion in Python: An Introduction (Quiz)
- Build a Hash Table in Python With TDD (Quiz)