Comparisons
Swaps
Pass

  • Compared
  • Smallest so far
  • Swapped
  • In place

More about selection sort

Selection sort

Selection sort builds the sorted list from the front. Each pass scans the values that are not sorted yet, keeps track of the smallest one, and swaps it into the first unsorted place. Play the sort at any speed, or step through it one comparison at a time, and watch the counters for comparisons, swaps, and passes. Press Play to watch the sort, or Step and Back to go one comparison at a time. Drag the step slider to jump anywhere. The tiles count comparisons, swaps and passes. Choose how many bars, how they start, or type your own numbers. With the board selected, the arrow keys step, the space bar plays and pauses, and Home and End jump to the start and the end.

How it works

  1. Choose how many bars and how they start: random, nearly sorted, reversed, or with few unique values. Or type your own numbers.
  2. Press Play, or use Step and Back to move one comparison at a time. Drag the step slider to jump anywhere in the sort.
  3. Watch the smallest value so far change as the scan goes on, and the swap that puts it in place at the end of each pass.

How selection sort works

Look at every value in the list and remember the smallest. Swap it with the first value: the first place is now final. Then do the same for the rest of the list, starting one place further on. After n − 1 passes every value is in place, and the last one is in place by default.

Many comparisons, few swaps

The scan never stops early, because only after looking at every remaining value can it be sure which one is the smallest. So selection sort makes exactly n(n − 1)/2 comparisons for n values, whether the list is sorted, reversed, or random. In return it makes at most n − 1 swaps, one per pass. That makes it a fair choice when moving a value costs far more than comparing two.

Frequently asked questions

How does selection sort work?

It repeatedly selects the smallest value that is not sorted yet and swaps it into the first unsorted place. After pass k the first k places hold the k smallest values in order.

How many comparisons and swaps does selection sort make?

Always n(n − 1)/2 comparisons for n values: 45 for 10 values, 780 for 40. The number of swaps is at most n − 1, one per pass, and fewer when the smallest value is already in place.

Why does a sorted list take as long as a reversed one?

Because each pass must look at every remaining value to be sure it has found the smallest. Unlike bubble sort and insertion sort, selection sort cannot tell from its comparisons that the list is already sorted.

Is selection sort stable?

Not in this usual form. A swap can carry a value past an equal one: in 2, 2, 1 the first 2 swaps with the 1 and ends up behind the second 2. A version that shifts values instead of swapping them is stable, at the cost of more moves.

Are my numbers sent anywhere?

No. The sort runs in your browser, and your last settings stay in this browser's local storage.

Ready to try it?

Open PictorX in your browser — no install, no credit card required to start.

Go to Editor