Time Complexity of All Sorting Algorithms (Comparison Table)

No sorting algorithm that works by comparing elements can do better than O(n log n) time in the average case or the worst case. Merge sort and heap sort reach O(n log n) in every case. Quicksort reaches it on average but drops to O(n²) on a bad sequence of pivots. Counting sort, radix sort, and bucket sort do not compare elements at all. They sort integers in a bounded range in O(n + k) time, where k is the size of that range.

The reason for the O(n log n) limit is counting. A list of n items has n factorial possible orders. Each comparison rules out at most half of the orders that remain, so at least log2 of n factorial comparisons are needed. That quantity grows as n log n. An algorithm that never compares elements is not bound by that argument.

Sorting Algorithm Complexity Table

Two terms are used in the table. A sort is stable if elements with equal keys keep the order they had in the input. Extra space is the memory needed beyond the input array, and a sort is in-place if that extra space is O(1) or O(log n).

AlgorithmBestAverageWorstExtra spaceStableIn-place
Bubble sortO(n)O(n²)O(n²)O(1)YesYes
Selection sortO(n²)O(n²)O(n²)O(1)NoYes
Insertion sortO(n)O(n²)O(n²)O(1)YesYes
Shell sortO(n log n)Depends on gaps, about O(n^1.25)O(n²)O(1)NoYes
Merge sortO(n log n)O(n log n)O(n log n)O(n)YesNo
QuicksortO(n log n)O(n log n)O(n²)O(log n)NoYes
Heap sortO(n log n)O(n log n)O(n log n)O(1)NoYes
Counting sortO(n + k)O(n + k)O(n + k)O(n + k)YesNo
Radix sortO(d(n + k))O(d(n + k))O(d(n + k))O(n + k)YesNo
Bucket sortO(n + k)O(n + k)O(n²)O(n + k)YesNo
TimsortO(n)O(n log n)O(n log n)O(n)YesNo

In the table, n is the number of elements and d is the number of digits in the longest key. The letter k is the range of key values, or the number of buckets for bucket sort. Bubble sort reaches O(n) only in the version that stops early when a pass makes no swaps. Shell sort's average depends on the gap sequence, and the O(n²) worst case is for the original sequence that halves the gap each round. Quicksort's O(log n) extra space is the recursion stack, and it is conventionally counted as in-place.

Notes on the Table

Why quicksort is used despite its O(n²) worst case. The worst case appears only when the pivot is repeatedly the smallest or largest element. A random pivot or a median-of-three pivot makes that sequence extremely unlikely. Quicksort works on one contiguous section of the array at a time, so it uses the processor cache well and does fewer memory writes than merge sort. On random data it is usually the fastest comparison sort in practice, which is why Java's sort for primitive arrays is a quicksort variant.

Why merge sort is chosen when stability matters. Stability matters whenever you sort by one field after sorting by another. Sorting orders by date and then by customer keeps the date order inside each customer only if the second sort is stable. Merge sort is stable and guarantees O(n log n) in the worst case, at the cost of O(n) extra memory. It also suits linked lists and data too large to fit in memory, because it reads and writes in sequence.

Why Timsort is the default in Python and Java. Timsort finds runs, which are stretches of the input that are already in order, and merges them. It therefore finishes in O(n) time on sorted or nearly sorted input while keeping the O(n log n) worst case and the stability of merge sort. Real data is often partly sorted, and objects usually need stability. Python's built-in sort and Java's sort for object arrays both use it. Java sorts primitive arrays with quicksort instead, because stability has no meaning for plain numbers.

Which Sort to Name in an Interview

The answer depends on the type of the input. One of four rules usually applies.

  1. General objects or data of unknown shape. Name merge sort or the language's built-in sort, which is Timsort in Python and Java: O(n log n), stable.
  2. Integers in a small known range. Name counting sort: O(n + k), which is linear when k is close to n.
  3. Many fixed-width integers or strings. Name radix sort: O(d(n + k)), linear when the number of digits is small.
  4. Tight memory, and stability does not matter. Name heap sort for a guaranteed O(n log n) with O(1) extra space, or in-place quicksort when average speed matters most.

State the rule, give the complexity, and say why the alternative was rejected. If the question is simply which sort runs fastest on a real machine rather than on paper, that is answered in Which sort is fastest?.

Interviewers rarely ask you to implement a sort from memory, but they do ask which one you would choose and why. The sorts that appear most often in that conversation are listed in the most-asked sorting algorithms. The wider set worth knowing is in the top sorting and searching algorithms for interviews.

How to Prepare

  • Memorize the table's middle three columns. Best, average, and worst case for the eleven sorts above. Be able to explain any cell that differs from its neighbors.
  • Implement merge sort and quicksort once by hand. Writing the merge step and the partition step is the fastest way to remember why one is stable and the other is not.
  • Practice the four-line rule. For each of five sample inputs, name the sort, its complexity, and the sort you rejected.
  • Study sorting inside a full data structures course. Grokking Data Structures and Algorithms for Coding Interviews covers every algorithm in the table with worked examples.
  • Then learn the patterns built on sorting. Grokking the Coding Interview teaches the merge intervals, two pointers, and top K patterns that assume a sorted input.
TAGS
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What Is the Cisco Interview Process Like? (Round by Round)
The Cisco software engineer interview: online assessment, recruiter screen, technical interviews over Webex, and a final loop of three to five rounds.
Which platform is best for DevOps?
What is an abstract class?
What is the goal of Datadog?
What is the principle of code review?
Why is processing a sorted array faster than processing an unsorted array?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$99

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
About Us
Our Team
Careers
Partners
Contact Us
Become Affiliate
Copyright © 2026 Design Gurus, LLC. All rights reserved.