Heapsort

What Are the Five Main Sorting Algorithms?

Five sorting algorithms that most intro courses cover: bubble, insertion, merge, quick and heap sort. See how each works, how fast it runs, and its tradeoffs.

On this page
  1. Short answer
  2. Bubble sort
  3. Insertion sort
  4. Merge sort
  5. Quicksort
  6. Heapsort
  7. Side-by-side comparison
  8. Which one should you pick?

Short answer

No single list is official, but most introductory courses teach the same core group of five: bubble sort, insertion sort, merge sort, quicksort, and heapsort. Each one takes an unsorted list and puts it in order, but they work in different ways and make different tradeoffs in speed, memory, and stability.

Other common sorts include selection sort and counting sort. Python’s built-in sort uses timsort, a hybrid of merge sort and insertion sort.

Bubble sort

Bubble sort walks through the list and swaps neighboring items that are out of order. It repeats these passes until a full pass makes no swaps, so the largest values move toward the end one pass at a time. Its average and worst-case time is O(n²). A version that stops as soon as a pass makes no swaps finishes in O(n) time on a list that is already sorted. It is stable and works in place.

Bubble sort is mostly taught for learning. Its quadratic running time makes it a poor choice for large lists.

Insertion sort

Insertion sort keeps a sorted section at the front of the list. It takes each new item and slides it left into the right position within that section. Its average and worst-case time is O(n²). On a nearly sorted list it takes O(n) time, because each item needs only a few moves. It is stable and is an in-place algorithm. Because it is simple and fast on small inputs, it is often used for short lists and inside some larger sorting routines.

Merge sort

Merge sort splits the list in half, sorts each half, and then merges the two sorted halves. It runs in O(n log n) time in every case, so its speed is predictable. It is stable. For arrays, the merge step needs extra memory proportional to the size of the list, so it is not in-place. It also fits linked lists well, because merging does not need random access to the items.

Quicksort

Quicksort picks a pivot value and rearranges the list so that smaller values come before the pivot and larger values come after. It then sorts those two parts the same way. Its average time is O(n log n). Its worst case is O(n²), which happens when pivots are chosen badly, though good pivot choices make that rare in practice. A typical implementation needs little extra memory beyond the recursion, and it is usually not stable. It is often one of the faster general-purpose sorts.

Heapsort

Heapsort builds a max-heap from the list, then repeatedly moves the largest value to the end and restores the heap on what remains. The heapsort algorithm has a guaranteed O(n log n) worst case and needs only O(1) extra memory. It is not stable. Its step-by-step version is covered in its own article.

Side-by-side comparison

Algorithm Average time Worst-case time Extra memory Stable
Bubble sort O(n²) O(n²) O(1) Yes
Insertion sort O(n²) O(n²) O(1) Yes
Merge sort O(n log n) O(n log n) O(n) Yes
Quicksort O(n log n) O(n²) O(log n) Usually no
Heapsort O(n log n) O(n log n) O(1) No

The memory figure for quicksort is for the recursion in a typical implementation. The stability column describes typical versions of each sort, not every possible variant.

Which one should you pick?

There is no single answer to what is the most efficient sorting algorithm, because the best choice depends on the data. Insertion sort suits small or nearly sorted lists. Merge sort is useful when you need stability or predictable timing. Quicksort is often fast on average. Heapsort is useful when you need a guaranteed bound with little extra memory and don’t need stability.

If stability matters for your task, the Stable column answers the question of which sorting algorithms are stable among these five. The O notation in the table is explained in the guide to big o data structures.

The complete guide to heapsort

How the Heapsort Algorithm Works
Read the guide

Keep exploring

Guides from other topics

All topics →