Menu
Coddy logo textTech

Selection Sort

Last updated

Selection sort divides the array into a sorted region on the left and an unsorted region on the right. On each pass it scans the unsorted region to find the smallest element, then swaps it into the first unsorted position - growing the sorted region by one. Press play above to watch the scan-and-swap, or step through it one comparison at a time.

Selection sort always does the same number of comparisons regardless of the input, but it performs at most n-1 swaps - far fewer than bubble sort - which can matter when writes are expensive.

Time & space complexity

CaseComplexityNotes
Best caseO(n²)Comparisons happen even if sorted
Average caseO(n²)Random order
Worst caseO(n²)Reverse-sorted
SpaceO(1)In-place
StableNoSwaps can reorder equal elements

Step by step

StepWhat happens
1Treat the whole array as unsorted.
2Scan the unsorted region to find the minimum element.
3Swap that minimum into the first unsorted position.
4Move the boundary one step right (that slot is now sorted).
5Repeat until only one element is unsorted.

Worked example

Sorting [5, 2, 4, 1]:

PassArrayAction
Start[5, 2, 4, 1]Whole array is unsorted.
1[1, 2, 4, 5]Scan [5, 2, 4, 1], min is 1 at index 3; swap it with index 0.
2[1, 2, 4, 5]Scan [2, 4, 5], min is 2 already at index 1; swap with itself.
3[1, 2, 4, 5]Scan [4, 5], min is 4 already at index 2; no move needed.
Done[1, 2, 4, 5]Only 5 is left, so it is already in place.

When to use selection sort

Use it whenAvoid it when
Writes are expensive - it does at most n-1 swaps.The array is large - O(n²) comparisons dominate.
You need a simple, easy-to-implement in-place sort.You need a stable sort that preserves the order of equal keys.
Memory is tight - it uses only O(1) extra space.The data is nearly sorted - it cannot finish early like insertion sort.
The dataset is tiny and predictable performance matters.Throughput matters - O(n log n) sorts like quicksort are far faster.

Selection Sort code

A clean, runnable Selection Sort implementation in Python, JavaScript, Java, C++, C. Pick a language, copy the code, or open it pre-loaded in the Coddy Playground.

Selection Sort code in Python

Python
1def selection_sort(a):2    n = len(a)3    for i in range(n - 1):4        # Find the smallest element in the unsorted tail5        min_idx = i6        for j in range(i + 1, n):7            if a[j] < a[min_idx]:8                min_idx = j9        a[i], a[min_idx] = a[min_idx], a[i]10    return a11
12
13nums = [64, 25, 12, 22, 11]14print("Before:", nums)15selection_sort(nums)16print("After: ", nums)
Run this code in the Python Playground

Selection Sort FAQ

What is the time complexity of selection sort?
Selection sort is O(n²) in all cases - best, average, and worst - because it always scans the entire unsorted region to find each minimum. It uses O(1) extra space.
Is selection sort stable?
The standard in-place version is not stable, because swapping a distant minimum into place can move an equal element past another. A stable variant exists but requires shifting instead of swapping.
When is selection sort useful?
It is useful when the cost of writing to memory is high, since it performs at most n-1 swaps - the minimum possible for a comparison sort that moves elements.
What is the difference between selection sort and bubble sort?
Both are O(n²) comparison sorts, but selection sort makes at most n-1 swaps while bubble sort can make up to O(n²) swaps. Bubble sort can also detect an already-sorted array and stop early, whereas selection sort always runs the full number of passes.
Should I use selection sort or insertion sort?
Prefer insertion sort in most cases - it is stable, runs in O(n) on nearly-sorted data, and is faster on average. Choose selection sort only when minimizing the number of writes is the priority, since it guarantees at most n-1 swaps.
Why does selection sort always run in O(n²) even on a sorted array?
Selection sort has no way to know an element is already the minimum without scanning the rest of the unsorted region, so it performs every comparison on every pass regardless of input order. This means the best case equals the worst case at O(n²) - unlike insertion or bubble sort, which can short-circuit.
Coddy programming languages illustration

Master algorithms with Coddy

GET STARTED