Data Structures & Algorithms Sorting Algorithms
💡
Exercise 55

Selection Sort 10 XP Easy

Ctrl+Enter Run Ctrl+S Save

Selection Sort works by repeatedly finding the minimum element from the unsorted portion and putting it at the beginning.

# [64, 25, 12, 22, 11] # Find min (11) → swap with index 0 → [11, 25, 12, 22, 64] # Find min in rest (12) → swap with index 1 → [11, 12, 25, 22, 64] # Find min in rest (22) → swap with index 2 → [11, 12, 22, 25, 64] # Find min in rest (25) → already in place → [11, 12, 22, 25, 64]

Time: O(n²) always (even if sorted). Space: O(1). It makes the minimum number of swaps (at most n-1), which can be useful in some cases.

💡 Pro tip: Understand this problem deeply — don't just memorize the code. Try explaining the approach out loud as if teaching a friend. If you can explain it simply, you truly understand it!

📋 Instructions
Implement `selection_sort(arr)` that sorts in-place. Sort `[64, 25, 12, 22, 11]` and print the result.
Outer loop i from 0 to n-1. Find the index of the minimum element in arr[i:]. Swap arr[i] with arr[min_idx].
🧪 Test Cases
Input
selection_sort([64, 25, 12, 22, 11])
Expected
[11, 12, 22, 25, 64]
Test case 1
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.