Big O Notation

Big O for Data Structures

A practical guide to Big O for data structures: how arrays, lists, hash tables, trees and heaps compare on search, insert and delete costs, with examples.

On this page
  1. What Big O measures
  2. Common growth rates
  3. Operation costs for common data structures
  4. Why average and worst cases differ
  5. Sorting costs
  6. How to read a Big O claim
  7. Where Big O stops helping

What Big O measures

Big O notation describes how the work an operation does grows as the amount of data grows. It leaves out constant factors and smaller terms, so it describes the shape of growth rather than an exact running time. For data structures, it tells you how the cost of operations such as lookup, insert and delete scales with the number of items stored.

People often start with a simple question: “What does Big-O stand for?” The letter has a history of its own, but in practice the notation is a shorthand for comparing how costs grow.

Common growth rates

These are the growth rates you will see most often, listed from cheapest to most expensive as the input gets larger:

  • O(1), constant: the cost does not change with the number of items. Reading an array element by its index is the usual example.
  • O(log n), logarithmic: the cost goes up by a fixed step each time the input doubles. Binary search on a sorted array is the standard example.
  • O(n), linear: the cost grows in step with the input. Checking every item in a list once is linear.
  • O(n log n), linearithmic: the usual cost of efficient comparison-based sorting.
  • O(n²), quadratic: the cost grows with the square of the input, as in nested loops that each visit every item.

Operation costs for common data structures

Beginners often ask “Is an array a data structure?” It is one of the simplest, and it makes a good baseline for the rest of the table. The table below gives typical costs for the usual implementations. Real libraries can differ in details, so check their documentation when you need exact guarantees.

Structure Lookup or search Insert or delete
Array O(1) by index; O(n) to search an unsorted array O(n) in the middle, because later items shift
Dynamic array (such as Python’s list) O(1) by index; O(n) to search O(1) amortized at the end; O(n) in the middle
Linked list O(n) to reach an item O(1) at the head, or after a node you already hold
Stack O(n) to search; push and pop use only the top O(1) for push and pop
Queue O(n) to search O(1) for enqueue and dequeue with a linked list or circular buffer
Hash table O(1) average; O(n) worst case O(1) average; O(n) worst case
Binary search tree O(log n) when balanced; O(n) when skewed O(log n) when balanced; O(n) when skewed
Balanced tree, such as AVL or red-black O(log n) worst case O(log n) worst case
Binary heap O(1) for the minimum or maximum; O(n) to find an arbitrary value O(log n) to insert; O(log n) to remove the top

Stacks and queues are usually taught together, and their operations are where Big O is easiest to see. Stack operations in data structure lessons are a good example: push and pop touch only the top item, so they stay O(1). A queue built on a plain array that removes items from the front by shifting everything left costs O(n) per removal, so the implementation matters as much as the structure.

Why average and worst cases differ

Some structures have an average cost far better than their worst cost. A hash table’s average O(1) lookup assumes keys spread evenly across buckets. If many keys land in the same bucket, a lookup may have to check many of them, which is O(n) in the worst case.

Trees show the same pattern. A binary search tree that stays balanced has O(log n) lookups, but one built from sorted input without rebalancing becomes a chain and degrades to O(n). Self-balancing trees exist to prevent that. The avl tree in data structure courses is the classic example, since its rebalancing keeps the height low and the worst case at O(log n).

Readers who ask “Is Big-O the worst-case?” are usually noticing this gap. Big O does not have to describe the worst case; the answer depends on which case the claim is about.

Sorting costs

Sorting is where these costs get compared most often. Bubble sort, insertion sort and selection sort are O(n²) in the average and worst cases. Merge sort and heapsort are O(n log n) in the average and worst cases. Quicksort is O(n log n) on average but can reach O(n²) in the worst case, when its pivots consistently split off very little.

The heapsort algorithm uses a binary heap to reach O(n log n) time in its best, average and worst cases, which makes it a useful contrast to quicksort. No comparison-based sort can beat O(n log n) in the worst case, which is why that bound is the usual benchmark.

Readers who ask “What is the most efficient sorting algorithm?” should first decide which input and which case matters for their data, because the answer changes with both.

How to read a Big O claim

When you see a Big O claim, check four things:

  • The case: best, average or worst. An unqualified claim often means the worst case.
  • The operation: what is being counted, such as comparisons, element moves or memory.
  • The constants: 3n and n are both O(n), so the constant is dropped.
  • The dominant term: n² + 100n is O(n²), because the squared term dominates as n grows.

Where Big O stops helping

Big O compares growth; it is not a stopwatch measurement. A few things it does not capture:

  • Small inputs: for a handful of items, constant factors and overhead can matter more than growth rates.
  • Hardware: arrays store items next to each other, which often runs faster in practice than linked nodes scattered through memory, even when the Big O is the same.
  • Memory: space use has its own Big O. A structure can be fast and still use far more memory.
  • Assumptions: hash table bounds depend on a good hash function and keys that spread well.

Big O for data structures gives you a common language for comparing choices. Use it to rule out options that will not scale, then measure the ones that remain on realistic data.

Keep exploring

Guides from other topics

All topics →