Back to All Algorithms
Selection Sort
Category: Sorting | Time: O(n^2) | Space: O(1)
Visualization
Visual representation of the data structure
About Selection Sort

An in-place comparison sorting algorithm. It divides the input list into two parts: a sorted sublist which is built up from left to right and a sublist of the remaining unsorted items that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.

Code Editor
The code is for reference. Editing it won't affect the visualization.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Variable State
{
  "arr": "[64, 25, 12, 22, 11]",
  "n": 5
}
Selection Sort: How It Works
Last updated October 28, 2026

Overview

Selection Sort splits the array into a sorted prefix and an unsorted suffix. On every pass it scans the whole unsorted suffix to find the smallest value, then swaps that value into the first unsorted slot. The sorted prefix grows by exactly one element per pass.

The key property is that it performs at most n - 1 swaps in total, no matter what the input looks like. The number of comparisons, however, is always about n^2/2, because finding a minimum requires looking at every remaining element.

With the default input 64, 25, 12, 22, 11, the first pass finds 11 and swaps it with 64; the second finds 12 and swaps it with 25. Watch min_idx in the variable inspector change as smaller values are discovered.

Step by step

  1. Set i = 0 as the boundary between the sorted and unsorted parts.
  2. Assume arr[i] is the minimum and remember its index in min_idx.
  3. Scan j from i + 1 to the end; whenever arr[j] < arr[min_idx], update min_idx.
  4. Swap arr[i] with arr[min_idx].
  5. Increase i and repeat until only one unsorted element is left.

Complexity

CaseCostWhy
Best caseO(n^2)Even sorted input needs a full scan to confirm each minimum.
Average caseO(n^2)n(n-1)/2 comparisons regardless of order.
Worst caseO(n^2)Same comparison count; swaps never exceed n - 1.
Extra spaceO(1)Only min_idx and a temporary for the swap.

When to use it

  • When writes are expensive compared with reads, for example flash memory or EEPROM, because it does the fewest swaps of the simple sorts.
  • Small arrays where predictable, input-independent running time is useful.
  • As a stepping stone to Heap Sort, which is selection sort with a faster way of finding the minimum.

Common mistakes

  • It is not stable: the long-distance swap can jump an element over an equal one. Sorting [2a, 2b, 1] moves 2a behind 2b.
  • Swapping inside the inner loop, every time a smaller value is seen, turns it into a slower, more confusing variant of Bubble Sort.
  • It never finishes early, so it is a poor choice when the input is often already sorted.
Worked Example: Selection Sort Dry Run
Generated from the same trace that drives the visualizer above, using the default input.

A dry run is the best way to check your understanding of Selection Sort: follow each row, predict the next one, and compare. The run below takes 47 steps in total. Steps that only re-print loop counters are left out so the decisions stand out. The Line column refers to the line numbers in the code listing above.

Input
64, 25, 12, 22, 11
Result
arr = [11, 12, 22, 25, 64], return value = [11, 12, 22, 25, 64]
StepLineArrayWhat happens
12[64, 25, 12, 22, 11]n = 5
56[64, 25, 12, 22, 11]n = 5, i = 0, min_idx = 0, j = 1, comparison = 25 < 64
86[64, 25, 12, 22, 11]n = 5, i = 0, min_idx = 1, j = 2, comparison = 12 < 25
116[64, 25, 12, 22, 11]n = 5, i = 0, min_idx = 2, j = 3, comparison = 22 < 12
136[64, 25, 12, 22, 11]n = 5, i = 0, min_idx = 2, j = 4, comparison = 11 < 12
1611[64, 25, 12, 22, 64]n = 5, i = 0, min_idx = 4, temp = 11, arr[min_idx] = 64
1712[11, 25, 12, 22, 64]n = 5, i = 0, min_idx = 4, temp = 11, arr[i] = 11
216[11, 25, 12, 22, 64]n = 5, i = 1, min_idx = 1, j = 2, comparison = 12 < 25
246[11, 25, 12, 22, 64]n = 5, i = 1, min_idx = 2, j = 3, comparison = 22 < 12
266[11, 25, 12, 22, 64]n = 5, i = 1, min_idx = 2, j = 4, comparison = 64 < 12
2811[11, 25, 25, 22, 64]n = 5, i = 1, min_idx = 2, temp = 12, arr[min_idx] = 25
2912[11, 12, 25, 22, 64]n = 5, i = 1, min_idx = 2, temp = 12, arr[i] = 12
336[11, 12, 25, 22, 64]n = 5, i = 2, min_idx = 2, j = 3, comparison = 22 < 25
366[11, 12, 25, 22, 64]n = 5, i = 2, min_idx = 3, j = 4, comparison = 64 < 22
3811[11, 12, 25, 25, 64]n = 5, i = 2, min_idx = 3, temp = 22, arr[min_idx] = 25
3912[11, 12, 22, 25, 64]n = 5, i = 2, min_idx = 3, temp = 22, arr[i] = 22
436[11, 12, 22, 25, 64]n = 5, i = 3, min_idx = 3, j = 4, comparison = 64 < 25
4714[11, 12, 22, 25, 64]return value = [11, 12, 22, 25, 64]
Frequently Asked Questions
About Selection Sort

How does Selection Sort work?

Selection Sort divides the input list into two parts: a sorted sublist which is built up from left to right, and a sublist of unsorted items. The algorithm proceeds by finding the smallest element in the unsorted sublist and swapping it with the leftmost unsorted element, moving the sorted sublist boundary one element to the right.

What is the time and space complexity of Selection Sort?

Selection Sort has a time complexity of O(n^2) in all cases (best, average, and worst) because it always makes n-1 passes and scans the remaining array in each pass. The space complexity is O(1) as it's an in-place sort.

Is Selection Sort stable?

The standard implementation of Selection Sort is not stable. It can change the relative order of elements with equal values because it might swap a minimum element with the first element of the unsorted part, jumping over an identical element.

What is a major advantage of Selection Sort?

Its primary advantage is that it minimizes the number of swaps, performing at most n-1 swaps. This is beneficial in scenarios where writing to memory is a costly operation.

How does Selection Sort compare to Insertion Sort?

Both are O(n^2) algorithms. Selection Sort consistently performs the same number of comparisons, whereas Insertion Sort performs fewer comparisons if the array is partially sorted. However, Selection Sort makes fewer swaps.

Is Selection Sort adaptive?

No, Selection Sort is not an adaptive algorithm. Its runtime doesn't change based on whether the input array is presorted or not; it will always perform the same number of comparisons.

Can Selection Sort be used on linked lists?

It's possible, but highly inefficient. Finding the minimum element in a linked list requires a full traversal, and swapping nodes is more complex than swapping array elements, negating its main advantage of minimal swaps.

Does the size of the data values affect Selection Sort's performance?

No, the performance of Selection Sort depends only on the number of elements in the list, not on the values of the elements themselves.

What is a 'double-ended' Selection Sort?

This is a variation where each pass finds both the minimum and maximum elements in the unsorted part and places them at the beginning and end, respectively. It can slightly reduce the number of passes but doesn't change the overall O(n^2) complexity.

Why might you choose Selection Sort for a small dataset?

For very small datasets, its simplicity and lack of recursion make it easy to implement and debug. While other algorithms are faster for larger sets, the performance difference is negligible for tiny arrays.

Where can I learn more?

You can find a more detailed explanation on GeeksforGeeks.