Sorting Efficiency

Is Selection Sort Always O(n²)?

For the standard version, yes: selection sort always does about n²/2 comparisons, so its running time is O(n²) for any input order, including sorted lists.

On this page
  1. Short answer
  2. How selection sort works
  3. Counting the comparisons
  4. Why the input order does not help
  5. Variants that behave differently
  6. When selection sort is still useful

Short answer

For the standard version of selection sort, yes. It always does about n(n − 1)/2 comparisons, so its running time is O(n²) in the best, average and worst cases. The exact count does not depend on how the items are arranged. Some variants change this, and they are covered below.

How selection sort works

Selection sort repeats one simple step. It scans the unsorted part of the list to find the smallest item, then swaps that item into the next position of the sorted part. It keeps doing this until the whole list is in order.

Take the list 5, 3, 8, 1:

  • Pass 1: The smallest item is 1, at the end. Swap it with the 5, giving 1, 3, 8, 5.
  • Pass 2: The unsorted part is 3, 8, 5. The smallest is 3, which is already in place. The list stays 1, 3, 8, 5.
  • Pass 3: The unsorted part is 8, 5. The smallest is 5, so swap it with 8, giving 1, 3, 5, 8.

Counting the comparisons

For a list of n items, the first pass compares the first item with each of the other n − 1 items to find the smallest. The second pass does n − 2 comparisons, and so on down to a single comparison. The total is (n − 1) + (n − 2) + … + 1, which equals n(n − 1)/2. That is roughly n²/2, which is O(n²).

The four-item list above needs 3 + 2 + 1 = 6 comparisons, which matches 4 × 3 ÷ 2.

Why the input order does not help

Selection sort cannot know which item is the smallest until it has looked at every remaining item, so it cannot skip work. A list that is already sorted still gets a full scan on every pass. This is different from insertion sort, which can finish a nearly sorted list in close to n comparisons, since it stops as soon as each item is in place.

The number of swaps is small, though. Standard selection sort makes at most n − 1 swaps, one per pass.

Variants that behave differently

Some versions add a check that stops the algorithm once the remaining part is already in order. With that check, a sorted list can finish in about n comparisons. That is a change to the algorithm, and the standard version has no such check.

Another variant finds both the smallest and the largest item on each pass. This halves the number of passes, but the total number of comparisons still grows like n², so it is still O(n²).

When selection sort is still useful

Selection sort makes few writes, so it can be a reasonable choice when writing to memory is expensive. For very small lists, its simplicity is also an advantage. For large lists, an algorithm such as merge sort or heapsort is far better, because it runs in O(n log n) time. For a broader comparison of sorts, see what is the most efficient sorting algorithm.

The O(n²) notation is explained in big o for data structures, if you want a refresher on reading these bounds.

The complete guide to sorting efficiency

What Is the Most Efficient Sorting Algorithm?
Read the guide

Keep exploring

Guides from other topics

All topics →