How do bubble sort, merge sort and quicksort compare?

When comparing sorting algorithms, the main differences come down to speed and memory. Bubble sort is simple to write but very slow for large lists. Merge sort is fast and consistently reliable, but it requires extra memory. Quicksort is usually the fastest in practice and saves memory, but it can occasionally slow down if you get unlucky with your data.

Think of organizing a scattered deck of cards. Bubble sort is like repeatedly swapping adjacent out-of-order cards until the whole deck is sorted. Merge sort is like splitting the deck exactly in half over and over until you have single cards, then perfectly weaving them back together. Quicksort is picking a "pivot" card, throwing smaller cards to its left and larger cards to its right, and repeating that process on the two piles.

Bubble Sort: Simple but Slow

Bubble sort works by stepping through the list, comparing adjacent elements, and swapping them if they are in the wrong order. It repeats this process until no more swaps are needed. While it is incredibly easy to understand, it is very inefficient. Its time complexity is O(n2)O(n^2), meaning that as the number of items (nn) grows, the time it takes grows quadratically. You will rarely use this outside of learning the basics.

Merge Sort: The Steady Divider

Merge sort is a "divide and conquer" algorithm. It cuts the list in half recursively until it reaches single elements, which are technically already sorted. Then, it merges those sub-lists back together in the correct order. The huge advantage is its guaranteed speed: it always runs in O(nlogn)O(n \log n) time, no matter what. The downside? It requires creating temporary arrays to hold the divided halves, meaning its space complexity is O(n)O(n).

Quicksort: Fast but Unpredictable

Quicksort also uses "divide and conquer," but it sorts in place. It chooses a "pivot" element and partitions the array so that everything smaller is to the left of the pivot and everything larger is to the right. It then recursively applies this to the left and right sections. On average, it runs in O(nlogn)O(n \log n) time and requires very little extra memory (O(logn)O(\log n) space). However, if it repeatedly picks a bad pivot (like the highest or lowest number), its worst-case time complexity drops to a sluggish O(n2)O(n^2).

Where Students Slip

A common mistake is assuming that because quicksort has a worse "worst-case" time than merge sort, it must be slower. In the real world, quicksort is almost always faster because its inner loops are highly optimized for computer architecture, and bad pivots are easily avoided by picking a random pivot. Students also mix up memory usage: remember that merge sort is the "memory hog" of the three.

Worked through

Trace the first full pass (one sweep through the array) of sorting the array [5, 3, 8, 4, 2] using Bubble Sort.

We will compare adjacent pairs from left to right and swap them if the left number is larger than the right number.

  1. Compare 5 and 3. Since 5 > 3, swap them. Array becomes: [3, 5, 8, 4, 2].
  2. Compare 5 and 8. Since 5 < 8, do nothing. Array stays: [3, 5, 8, 4, 2].
  3. Compare 8 and 4. Since 8 > 4, swap them. Array becomes: [3, 5, 4, 8, 2].
  4. Compare 8 and 2. Since 8 > 2, swap them. Array becomes: [3, 5, 4, 2, 8].

After one full pass, the largest number (8) has "bubbled" to the very end of the array. The algorithm would then repeat this process for the remaining unsorted numbers.

Questions students ask

Ask about this topic

Where this comes from: Introduction to Algorithms by Cormen, Leiserson, Rivest, and Stein · Khan Academy: Algorithms Unit · OpenStax: Data Structures and Algorithms

See also