Oh My Algorithm
Algorithm Guidecomplexity: O(n²)

Selection Sort

An intuitive in-place algorithm that repeatedly finds the smallest (or largest) element and swaps it to the front.

01Selection Sort

Starting selection sort. Repeatedly find the smallest value in the unsorted region and send it to the front.

Choose what goes in the first slot. Take the leading 45 as the candidate and scan everything to its right.

During the scan 12 takes over as the candidate, then 10. The smallest value overall is the 10 at the very end.

Send that 10 to the first slot. The 45 that sat there moves to where 10 was.

Next is the second slot. The smallest remaining value, 12, is already there, so nothing moves.

Time to fill the third slot. Look for the smallest among the remaining 89 · 34 · 67 · 23 · 56 · 45.

The smallest is 23. Get ready to swap it with the 89 currently holding that slot.

Send 23 to the third slot. 89 falls back to where 23 was.

The fourth slot holds 34, the smallest of what remains, so again there is no swap.

The fifth slot takes 45, the smallest left. Swap it with the 67 sitting there.

The left five slots are settled. Each slot filled grows the sorted region by one.

The sixth slot takes 56. Swap it with the 89 holding that slot.

Only 89 and 67 are left out of order. Swap the two.

Selection sort is done. It swaps just once per slot, but scans the whole remaining region every time to find the minimum.

45
12
89
34
67
23
56
10
1 / 14

02 Understand It Simply

For Everyone
🔑How It Works

Finds the smallest value in the remaining range and swaps it to the front. Uses only n-1 swaps.

💡In Plain Words

Finds the minimum each pass and fills from the front.

Few swaps, but always O(n²) comparisons.

📍Where It's Used
  • When swap cost is high
  • learning sorting concepts

03 Python Implementation

A clean, readable reference implementation of the core logic of Selection Sort.

core_implementation.py
def selection_sort(arr):
    n = len(arr)
    for i in range(n):
        min_idx = i
        for j in range(i + 1, n):
            if arr[j] < arr[min_idx]:
                min_idx = j
        arr[i], arr[min_idx] = arr[min_idx], arr[i]
    return arr

04 Frequently Asked Questions

FAQ
What is Selection Sort?+

An intuitive in-place algorithm that repeatedly finds the smallest (or largest) element and swaps it to the front.

What is the time complexity of Selection Sort?+

The time complexity of Selection Sort is O(n²). Follow the step-by-step visualization to see exactly why.

Where is Selection Sort used?+

When swap cost is high, learning sorting concepts.

What's a simple analogy for Selection Sort?+

Finds the smallest value in the remaining range and swaps it to the front. Uses only n-1 swaps.