AlgorithmTime complexitySpace complexity
BestAverageWorstAuxiliary, worst
QuicksortaΘ(n log n)Θ(n log n)Θ(n2)O(log n)
MergesortΘ(n log n)Θ(n log n)Θ(n log n)O(n)
TimsortΘ(n)Θ(n log n)Θ(n log n)O(n)
HeapsortbΘ(n log n)Θ(n log n)Θ(n log n)O(1)
Bubble sortcΘ(n)Θ(n2)Θ(n2)O(1)
Insertion sortΘ(n)Θ(n2)Θ(n2)O(1)
Selection sortΘ(n2)Θ(n2)Θ(n2)O(1)
Tree sortdΘ(n log n)Θ(n log n)Θ(n2)O(n)
Shell sorteΩ(n log n)Depends on gapsO(n2)O(1)
Bucket sortfΘ(n+k)Θ(n+k)O(n2)O(n+k)
Radix sortΘ(nk)Θ(nk)Θ(nk)O(n+b)
Counting sortΘ(n+k)Θ(n+k)Θ(n+k)O(n+k)
CubesortΩ(n)Θ(n log n)O(n log n)O(n)

Notation in this table

  1. Quicksort space. O(log n) when the recursion handles the smaller partition first. A naive implementation can need O(n) stack space in the worst case.
  2. Heapsort. The Θ(n log n) best case holds for distinct keys. Input where every key is equal can finish in O(n).
  3. Bubble sort. The Θ(n) best case needs an early exit when a pass makes no swaps. Without it, every case is Θ(n²).
  4. Tree sort. Assumes inserting into a plain binary search tree, which degrades to Θ(n²) on already-sorted input. A self-balancing tree makes the worst case Θ(n log n).
  5. Shell sort. The running time depends on the gap sequence. Shell's original gaps give O(n²) in the worst case; sequences such as Pratt's give O(n (log n)²). Average-case bounds are not known for most sequences.
  6. Bucket sort. The Θ(n+k) average assumes input spread roughly evenly across buckets. If most items land in one bucket, the cost is dominated by sorting that bucket, up to O(n²).

Topics on this page

Each one has its own page with an explanation, code, and the mistakes people make.

Keyboard shortcuts

/
Search topics
?
Show this list of shortcuts
r
Open the revision sheet
b
Save or unsave the focused card, or the topic you are reading
l
Mark the focused card, or the topic you are reading, as learned
Space Left arrow Right arrow
In the visualizer: play or pause, step back, step forward
1 to 4 Enter
In the quiz: pick an answer, then go to the next question
Escape
Close this list