Sorting by deliberate selection

Find the smallest.
Place it next.

Selection Sort scans the unsorted region, remembers its smallest value, and moves that value to the next open position.

Mini example[ 5, 2, 4 ]
01
524
find 2
02
254
place
03
245
sorted

Key idea After each pass, the sorted region grows by exactly one value from left to right.

Before you start

What you will learn

A clear finish line for the lesson, plus the small amount of knowledge you need before beginning.

Learning objectives
  1. Explain how the sorted and unsorted regions change after each pass.
  2. Trace the current minimum while scanning an array.
  3. Implement Selection Sort in JavaScript, Python, Java, or C++.
  4. Recognize its fixed comparison cost, low swap count, and lack of stability.
01Understand02Visualize03Trace04Code05Complexity06Quiz07Challenge08Use cases

01 — The idea

How Selection Sort thinks

It repeats one task: “Which value is smallest in the part I have not sorted yet?”

1
273

Mark the boundary

The first unsorted position is where the next smallest value belongs.

2
7min?3

Find the minimum

Scan every remaining value and remember the smallest one seen.

3
3⇄7

Place and repeat

Swap the minimum into the boundary, then move the boundary one step right.

02 — Try it yourself

Watch the minimum move into place

Use your own numbers, then follow the boundary, candidate comparisons, and selected minimum.

3–10 values, each from 1 to 99
Pass — Paused
→

Ready. Begin at the first unsorted position.

Comparisons0
Swaps0
Locked0/7
Comparing Current minimum Sorted
Dry-run trace

Turn each animation into a written decision

The latest eight Selection Sort decisions
StepPassActionCandidates / minimumArray after stepSwaps
0—Ready—[64, 25, 12, 22, 11, 48, 36]0

The highlighted row always matches the visualization above. Step forward to extend the trace.

03 — Read the code

From minimum scan to working code

Compare how all four languages remember one index during the scan and swap only after the pass.

selection-sort.mjs
01function selectionSort(numbers) {02  const array = [...numbers];03 04  for (let boundary = 0; boundary < array.length - 1; boundary++) {05    let minIndex = boundary;06 07    for (let i = boundary + 1; i < array.length; i++) {08      if (array[i] < array[minIndex]) {09        minIndex = i;10      }11    }12 13    if (minIndex !== boundary) {14      [array[boundary], array[minIndex]] = [array[minIndex], array[boundary]];15    }16  }17 18  return array;19}

04 — Avoid the traps

Common mistakes

These bugs often produce an answer that looks almost right. Learn the symptom and the correction together.

01

Swapping during the scan

Symptom: Values move every time a smaller candidate appears.

Correction: Remember the minimum index and perform at most one swap after the scan.

02

Resetting the minimum incorrectly

Symptom: A pass compares against an old minimum from the previous pass.

Correction: Initialize the minimum index to the current boundary at the start of every pass.

03

Skipping the last candidate

Symptom: The smallest value at the right edge is never selected.

Correction: Scan from boundary + 1 through the final array index.

04

Assuming sorted input is faster

Symptom: The best case is reported as linear time.

Correction: Selection Sort still scans the full unsorted region on every pass, even when no swaps occur.

05 — Check yourself

Check your understanding

Answer 4 questions, then reveal the reasoning. Your progress stays on this device—no account required.

0 of 4 answered
Q1What is guaranteed after the first pass?
Q2What is the best-case time complexity for standard Selection Sort?
Q3How many swaps can Selection Sort perform at most for n values?
Q4After the first pass on [4, 2, 3, 1], what is the array?

Answer every question to check your work.

06 — Mini game

Choose the minimum

Build the sorted region exactly as Selection Sort does by choosing the smallest package still available.

Rule At each boundary, select the smallest package in the unsorted region. A correct choice moves into place automatically.
Pass 1: find the minimum0 picks
↓

Choose the smallest value from index 0 onward.

07 — Use it wisely

Where Selection Sort fits

◎

Learning

Its boundary and minimum pointer make loop invariants concrete and visible.

Great fit
⇄

Few writes

It performs at most n − 1 swaps, useful when writes cost more than comparisons.

Useful property
▦

Tiny datasets

For a handful of values, its simple in-place structure may be sufficient.

Reasonable fit
×

Large or nearly sorted data

It always performs quadratic comparisons and does not benefit from existing order.

Avoid

Lesson progress

Finish this lesson with confidence

Use this private checklist to confirm what you learned. Progress stays only in this browser.

0 of 4 complete0%

Complete every checklist item to finish the lesson.