2026-05-15
Written by: Juri Breslauer
Algorithm Notes
Selection sort splits the array into a sorted part and an unsorted part. On every pass, it finds the smallest value in the unsorted part and moves it into the next open position on the left.
Interactive demo
The algorithm fills the array from left to right. For position 0, it searches
for the smallest value in the whole array. Then for position 1, it searches
for the smallest value in the remaining part, and so on.
Unlike bubble sort, values do not move through a chain of neighboring swaps. Selection sort first finds the right value, then performs one swap at the end of the pass.
The demo uses a small set of bars as an example:
Selection sort always performs about the same number of comparisons: O(n²)
in the best, average, and worst case. It does relatively few swaps: at most
n - 1. That makes it useful for learning, but larger arrays usually need a
faster sorting algorithm.
Here are examples of the same selection sort algorithm in different programming languages.