Skip to content

Selection Sort Visualizer

Each pass finds the smallest remaining value and swaps it to the front of the unsorted part.

Comparisons: 0 Swaps: 0 Values: 0
  • Unsorted
  • Comparing
  • Swapping
  • Current minimum
  • Sorted
Step 1 of 1

selectionSort(a):  for i = 0 to n − 2    min = i    for j = i + 1 to n − 1      if a[j] < a[min]        min = j    swap a[i] and a[min]
Time and space complexity
MeasureValue
Best caseO(n²)
Average caseO(n²)
Worst caseO(n²)
Extra spaceO(1)
StableNo
In placeYes

Selection sort splits the array into a sorted part on the left and an unsorted part on the right. In each pass it scans the unsorted part for the smallest value, then swaps it into the first unsorted position. This visualizer shows the scan with the current minimum in purple and each candidate in orange, so you can see the minimum change as smaller values turn up.

Watch the counters: selection sort always makes the same number of comparisons, about n²/2, whatever the input, but at most n − 1 swaps. That makes it useful when writing to memory is much more expensive than reading.

How to use the Selection Sort Visualizer tool

  1. Pick an array size and an input: random, nearly sorted, reversed or few unique values. You can also type your own numbers.
  2. Press Play to watch the sort, or Step to move one comparison or swap at a time. Back goes one step back.
  3. Follow the colours: orange bars are being compared, red bars are being swapped or written, green bars are in their final place.
  4. Read the message under the controls and the highlighted pseudocode line to see exactly what the algorithm is doing.
  5. Change the speed, switch the code tab to C++, Java, Python or JavaScript, and compare the counters with other inputs.

Why use ToolOrca Selection Sort Visualizer

  • Animated bars with play, pause, step and step back
  • Speed slider and a position slider to jump to any step
  • Comparison and swap counters
  • Random, nearly sorted, reversed and few-unique inputs, or your own numbers
  • Highlighted pseudocode line for every step
  • Code in C++, Java, Python and JavaScript
  • Works on phones and tablets, keyboard accessible

How selection sort works

For i from 0 to n − 2, set min = i, then compare a[min] with every a[j] for j from i + 1 to n − 1, updating min whenever a smaller value appears. After the scan, swap a[i] with a[min]. Now positions 0 to i hold the i + 1 smallest values in order, and the next pass starts one position further right.

Time and space complexity of selection sort

The scans make (n − 1) + (n − 2) + … + 1 = n(n − 1)/2 comparisons in every case, so the time is O(n²) best, average and worst; a sorted input does not help. It makes at most n − 1 swaps, sorts in place with O(1) extra memory and is not stable: the long-distance swap can jump a value over an equal one. For example, sorting [4a, 4b, 1] swaps 4a to the end, behind 4b.

When to use selection sort

Selection sort is simple and does very few writes, which can matter for flash memory or when moving an element is costly. For general use, insertion sort is faster on small or nearly sorted arrays, and O(n log n) sorts are far faster on large ones. Heap sort is selection sort with a heap to find the minimum (or maximum) quickly.

Every visualizer on this site runs in your browser with the same controls: Play, Pause, Step forward, Step back and a speed slider. You can also drag the position slider to jump to any step, or focus the drawing and use the Space bar and arrow keys.

Frequently asked questions

What is the time complexity of selection sort?

O(n²) in the best, average and worst case, because it always scans the whole unsorted part.

Is selection sort stable?

Not in its usual form. Swapping the minimum into place can move an element past another element with the same value.

How many swaps does selection sort make?

At most n − 1, one per pass, which is fewer than bubble sort or insertion sort.

What is the difference between selection sort and bubble sort?

Both are O(n²), but selection sort swaps once per pass after finding the minimum, while bubble sort swaps neighbours many times per pass. Bubble sort can stop early on sorted input; selection sort cannot.