Kazi Rahamatullah
Kazi Rahamatullah
AboutProjectsBlogContact
UsesBooks
ResumeView CV
Kazi Rahamatullah

© Copyright 2026 Kazi Rahamatullah

AboutProjectsBlogBooksUses
Twitter/XGitHubProduct HuntCodeSandbox
Back to Blog
DSASortingSelection SortAlgorithms

Selection Sort — Algorithm Visualized

Learn selection sort — find minimum each pass, swap to front, O(n²) comparisons, fewer swaps than bubble sort, with interactive step visualizer.

Jul 4, 20263 min read

Introduction

Selection sort divides the array into a sorted prefix and unsorted suffix. Each pass finds the minimum of the suffix and swaps it into the next position — at most n − 1 swaps, but still O(n²) comparisons.

Quick index

#Section
1How it works
2Interactive visualizer
3Full implementation
4Complexity analysis
5Real-life examples

1. How it works

plaintext
[ sorted | unsorted ]
   ↑         ↑
  arr[i]   scan for min → swap

Each pass: scan arr[i..n-1], swap minimum to arr[i]. Sorted region grows by one.

Back to index


2. Interactive visualizer

Selection Sort — Step Visualizer

Find the minimum each pass and swap it into the growing sorted prefix.

Step 1 / 9 — Unsorted

Unsorted

5
0
3
1
8
2
1
3
9
4
2
5
7
6

n = 7 elements

Step 1 — Input array

for (let i = 0; i < n - 1; i++)
  minIdx = argmin(arr[i..n-1])
  swap(arr[i], arr[minIdx])

Select minimum from unsorted suffix each pass.

Time

O(n²)

Space

O(1)

Stable

No

Back to index


3. Full implementation

js
function selectionSort(arr) {
  const n = arr.length
  for (let i = 0; i < n - 1; i++) {
    let minIdx = i
    for (let j = i + 1; j < n; j++) {
      if (arr[j] < arr[minIdx]) minIdx = j
    }
    if (minIdx !== i) [arr[i], arr[minIdx]] = [arr[minIdx], arr[i]]
  }
  return arr
}

Function calls with output

js
// ── Demo calls — output shown on the right ──
const arr = [5, 3, 8, 1, 9, 2, 7]
 
selectionSort(arr) // → [1, 2, 3, 5, 7, 8, 9]
selectionSort([3, 1, 2]) // → [1, 2, 3]

Back to index


4. Complexity analysis

MetricValue
TimeO(n²) always — Θ(n²) comparisons
SpaceO(1)
SwapsO(n) — at most one per pass
StableNo — long-distance swaps can reorder equals

Interview Answer

"Selection vs bubble sort?"

Both O(n²). Selection does fewer swaps (O(n) vs O(n²)) but the same number of comparisons. Selection is not stable; bubble is.

Back to index


5. Real-life examples

ScenarioWhy selection sort
Flash/EEPROM memoryMinimizes writes — flash cells wear out; at most n swaps
Sorting by cost on a budgetFewer expensive swap operations when swap cost >> compare cost
Small offline devicesSimple logic, no extra buffer — O(1) space
Teaching "find minimum" patternCore loop mirrors Math.min(...arr) by hand
Kiosk with fixed item count12 menu items sorted by price once at startup

Kiosk menu prices

15 items sorted once on boot — selection sort minimizes flash writes.

8
4.99
1.25
6.75
2.5

Index → value

5 kiosk items — unsorted prices in flash memory.

Real-world implementation

function selectionSort(arr) {
  const a = [...arr]
  for (let i = 0; i < a.length; i++) {
    let minIdx = i
    for (let j = i + 1; j < a.length; j++) {
      if (a[j] < a[minIdx]) minIdx = j
    }
    if (minIdx !== i) [a[i], a[minIdx]] = [a[minIdx], a[i]]
  }
  return a
}

Full working code for the scenario above.

Function calls with output

// ── Function calls with output ──
const menuPrices = [8.00, 4.99, 1.25, 6.75, 2.50]

Side output shows return value after each call — step through to trace execution.

Back to index


Summary

Selection sort minimizes writes — useful when swap cost dominates. Still O(n²) for comparisons.

Next reads: Insertion Sort · Merge Sort

Share this article

XLinkedInFacebook
Kazi Rahamatullah

Written by

Kazi Rahamatullah

FullStack Developer

X / TwitterGitHubLinkedIn

Subscribe to my newsletter

Stay up to date and get notified when I share new contents.

No spam ever, unsubscribe anytime