DSA Visualizer
SORT

Selection Sort Visualizer

Watch each pass hunt for the smallest value left and drop it into place. Press play, or step through one comparison at a time.

Finds the smallest remaining value each pass and places it. Always O(n²), but does the fewest swaps.

8
3
12
5
9
1
6
11
2
7
Selection sort scans the unsorted part for the smallest value each pass, then drops it into place.
0
ComparesComparisons
0
SwapsSwaps
0
WritesWrites
1 / 88

Pseudocode

Selection Sort
1for i in 0 .. n-1:
2 min = i
3 for j in i+1 .. n-1:
4 if a[j] < a[min]:
5 min = j
6 swap(a[i], a[min])

Legend

ComparingCurrent minSwappingSorted
Data

Tip: Space play/pause · ← → step

Learn selection sort

Selection Sort scans everything that is still unsorted, remembers where the smallest value is, and swaps it into the next free slot, so it makes at most one swap per pass.

Like bubble sort and insertion sort, it is a comparison sort: it decides what goes where purely by comparing pairs of values.

All three are also in-place (they reuse the same array, O(1) extra memory) and simple to reason about. The trade-off is speed: each runs in O(n²) on average, so they shine on small or nearly-sorted inputs rather than huge datasets.

New to this O(…) notation? Start with the Big-O Playground →

Stable

Keeps equal values in their original relative order. Bubble & insertion are stable; selection is not.

In-place

Sorts within the array itself — no second array needed, so memory stays at O(1).

How selection sort works

Imagine sorting a pile of coins by value. You look over every coin that is left, find the smallest, and put it at the front. Then you do the same with the rest. That is selection sort: each pass selects the smallest remaining value and drops it into the next free slot.

The list is always split in two. On the left is the sorted part, which starts empty and grows by one value per pass. On the right is everything still waiting. Nothing in the sorted part ever moves again, because every value in it is smaller than everything to its right.

Like bubble sort, all it needs is a way to compare two values. The starting order makes no difference to how much work it does, and that turns out to be both its weakness and its most interesting feature.

A worked example: sorting 5, 2, 4, 1, 3

Type 5, 2, 4, 1, 3 into the custom array box above and step through it. At the end of each pass the list looks like this:

Selection sort on the list 5, 2, 4, 1, 3, pass by pass
WhenThe listWhat happened
Start5 2 4 1 3Nothing is sorted yet.
After pass 11 2 4 5 34 comparisons. The smallest value is 1, so it swaps places with the 5 at the front.
After pass 21 2 4 5 33 comparisons. The smallest value left is 2 and it is already in position, so nothing moves.
After pass 31 2 3 5 42 comparisons. The 3 swaps with the 4.
After pass 41 2 3 4 51 comparison. The 4 swaps with the 5, and the one value left over must be the largest.

That is 10 comparisons but only 3 swaps. Bubble sort needs 7 swaps for the same list. Watch the Swaps counter to see the difference.

What i, j and min mean

i is the slot being filled. Everything to the left of i is already sorted.

j is the scanner. It starts at i + 1 and walks to the end of the list, looking at one value per step.

min is the important one. It holds the position of the smallest value seen so far in this pass, not the value itself. It starts out equal to i and changes whenever a[j] is smaller than a[min]. This is the first loop many beginners meet that has to remember something from one step to the next.

How many steps does selection sort take?

The first pass compares the candidate with all n − 1 other values, the second pass with n − 2, and so on down to 1. For 5 values that is 4 + 3 + 2 + 1 = 10. For 10 values it is 45, and for 20 it is 190.

The total is always exactly n × (n − 1) ÷ 2. It grows with n × n, which is written O(n²), and there is no lucky case: selection sort has no way to notice that the list is already in order, so best, average and worst are all the same.

Best
O(n²)
Average
O(n²)
Worst
O(n²)
Extra memory
O(1)
Stable
No

New to this notation? Start with the Big-O Playground →

When selection sort is the right choice

When moving data costs far more than looking at it. Writing to some kinds of memory is slow or wears the hardware out, and selection sort never makes more than n − 1 swaps. It is also a good fit for very small lists and for situations where you want the running time to be the same every time.

It also teaches an idea you will reuse everywhere: finding the smallest (or largest) value by scanning with a running best. That single loop is the heart of selection sort.

Mistakes beginners make

  • Storing the value instead of its position. If min holds the smallest value, you know what to move but not where it is. Keep the index.
  • Swapping inside the inner loop. Swapping every time a smaller value turns up still sorts the list, but it throws away the whole point of the algorithm. Swap once, after the scan.
  • Starting the scan in the wrong place. j begins at i + 1. Starting at 0 drags sorted values back into play.
  • Expecting it to be stable. If the order of equal values matters, use insertion sort instead.

Selection sort questions

Why is selection sort always O(n²), even on a sorted list?

Because it cannot know a value is the smallest without looking at every value that is left. It makes the same n × (n − 1) ÷ 2 comparisons whether the list is shuffled, reversed or already in order. Try the Sorted preset: the Comparisons counter ends on the same number as it does for a random list.

Why is selection sort not stable?

Its swap moves a value a long way in one jump, and that jump can carry it past an equal value. Sort 4a, 4b, 1 and the first pass swaps 1 with 4a, leaving 1, 4b, 4a. The two 4s have traded places.

How many swaps does selection sort make?

At most n − 1, one per pass, and fewer when a value is already where it belongs. That is the fewest of the three simple sorts. Bubble sort can need one swap for every out-of-order pair.

Selection sort or insertion sort: which is better?

Insertion sort, most of the time. It finishes early on lists that are nearly sorted, and it is stable. Selection sort wins only when writing a value is far more expensive than comparing two values.

Does selection sort need extra memory?

No. It sorts inside the list it was given and needs only a few variables (i, j and min), so its extra memory is O(1) however long the list is.