Selection sort.
- Time (worst)
- O(n²)
- Space
- O(1)
Find the smallest. Put it first. Repeat with the numbers left.
Why learn selection sort?
Selection sort puts numbers in order by choosing the smallest number from the part that still needs sorting. It checks that whole part, then makes at most one swap to put the chosen number first. It is useful for learning, but it needs many comparisons on large lists.
Customize input
Smallest → biggest
0 comparisons · 0 swaps
Start
Find the smallest. Put it first.
We'll put the numbers in order from smallest to biggest. Find the smallest number in the unfinished part, then put it in the first unfinished position.
Use Next for one action, or Play to watch. Drag the timeline to preview silently.
Voice is unavailable in this browser. Follow the explanations above.
Follow the logic
Pythondef selection_sort(a): for start in range(len(a) - 1): min_index = start for j in range(start + 1, len(a)): if a[j] < a[min_index]: min_index = j if min_index != start: a[start], a[min_index] = a[min_index], a[start] return an is the number of items. Best, average, and worst-case time: O(n²). This version still checks every remaining number even if the list is already sorted. It makes at most n − 1 swaps. Equal values may change their original order during a swap. O(1) space describes the algorithm; playback stores extra snapshots for rewinding.

