Productivity and Utilities

Sorting Algorithm Operation Counter

Enter a list of numbers to count the work of six sorting methods.

Your numbers

Example list. Separated by spaces, commas or line breaks, up to 500 numbers.

Result

Fewest comparisons

Merge sort: 16

8 numbers. All six methods give the same sorted list.

Numbers
8
Pairs in the list, n(n-1)/2
28
Sorted list
5 8 10 13 14 22 29 37

Work done by each method

MethodComparisonsSwaps or movesRounds
Bubble sort28187
Insertion sort23187
Selection sort2867
Merge sort16247
Quick sort1965
Heap sort25187

Bubble sort

RoundList after the round
110 14 29 13 5 22 8 37
210 14 13 5 22 8 29 37
310 13 5 14 8 22 29 37
410 5 13 8 14 22 29 37
55 10 8 13 14 22 29 37
65 8 10 13 14 22 29 37
75 8 10 13 14 22 29 37
How this was calculated
  1. Each method is run on its own copy of the list and every comparison of two items is counted. A swap exchanges two items; an insertion shift or a merge write moves one item.
  2. A round is one pass for bubble and selection sort, one insertion for insertion sort, one merge for merge sort, one partition for quick sort (last item as the pivot, Lomuto scheme) and one extraction for heap sort. Bubble sort stops early after a pass with no swaps.
  3. Counts depend on the order of the input, not just its size. A sorted list is the best case for bubble and insertion sort, and a reversed list is a worst case for them.

Calculated using SumPanda.com · https://sumpanda.com/sorting-algorithm-operation-counter/

Results are estimates for general information. Verify important figures before relying on them.

Next step

How to use the sorting algorithm operation counter

  1. Enter your numbers, separated by spaces, commas or line breaks. Up to 500 can be used.
  2. Pick a method to see its rounds in detail.
  3. Read the table of comparisons, swaps or moves, and rounds for all six methods, with the fewest comparisons in the headline.
  4. Try a sorted list, a reversed list and a list with repeats to see how the order of the input changes the work.

Formula

comparisons = number of times two items are compared

swaps or moves = swaps (bubble, selection, quick, heap), shifts (insertion), writes back (merge)

rounds = passes (bubble, selection), insertions, merges, partitions or extractions

For n items there are n(n-1)/2 pairs. Bubble, insertion and selection sort do work that grows with n squared in bad cases, while merge, quick and heap sort grow about as n times the logarithm of n.

Eight numbers

The list 29 10 14 37 13 5 22 8 has 8 x 7 / 2 = 28 pairs and 18 pairs out of order. Bubble sort makes 28 comparisons and 18 swaps in 7 rounds, insertion sort 23 comparisons and 18 shifts, and selection sort 28 comparisons but only 6 swaps.

Merge sort needs 16 comparisons, quick sort with the last item as pivot 19 comparisons and 6 swaps, and heap sort 25 comparisons. The fewest is merge sort with 16. All six methods give the sorted list 5 8 10 13 14 22 29 37.

Reading the counts

Swaps in bubble sort equal the number of pairs that are out of order, and insertion sort's shifts equal that number too, which is why both show 18 above. Selection sort always makes the same number of comparisons, because it scans for the smallest item every time, but it moves items very little. Insertion sort is very fast on lists that are nearly sorted.

Real programs also care about memory use, stability and how the cache behaves, none of which a count shows. To sort a table of data, use the Sort CSV by Column tool, and to see how the number of steps grows with the size of the input, use the Algorithm Growth Calculator.

Exact rules used

Bubble sort stops early after a pass with no swap. Insertion sort compares each item with those before it until one is not larger. Merge sort splits in the middle and counts every comparison while merging. Quick sort uses the last item as the pivot with the Lomuto scheme. Heap sort builds a max heap, then repeatedly moves the top to the end. Counting rules change the numbers, so compare only counts made with the same rules.

Assumptions and limits

  • Numbers are compared by value, and equal numbers are not exchanged by bubble and insertion sort, so those two methods are stable.
  • A swap of an item with itself is not counted.
  • Only the first 40 rounds of the chosen method are shown.
  • Decimals and negative numbers are allowed.
  • The tests check all outputs against built in sorting and the counts against separate instrumented code.

Formulas reviewed October 10, 2026. See the calculation methodology for how SumPanda rounds, tests and sources its formulas.

Frequently asked questions

Why is selection sort's comparison count the same for any list?

It always scans the whole unsorted part to find the smallest item, whatever the order, so it makes exactly n(n-1)/2 comparisons.

Why does bubble sort take only n-1 comparisons on a sorted list?

It stops after the first pass if nothing was swapped, and that pass makes n-1 comparisons.

Which method is best?

It depends on the data. Insertion sort suits nearly sorted lists, merge sort has steady performance, and quick sort is fast on average but can be slow with unlucky pivots.

Why do the counts change if I change the pivot rule?

Quick sort splits the list around the pivot, so a different pivot gives different parts and a different count. The page documents the last item rule so counts are repeatable.