Foundations
Linear Structures
Hashing
Array and String Patterns
Recursion, Search, and Sorting
Problem Solving Patterns
Algorithm Searching
Sorting Algorithms
Array Algorithms
Grid Algorithms
Linear Structure Algorithms
Hashing Algorithms
Recursion and Backtracking
Greedy Algorithms
Tree and Heap Algorithms
Graph Algorithms
Dynamic Programming Algorithms
String Algorithms
Math Algorithms
Keyboard ready
Space plays. Arrow keys step. R restarts.
LAB Selection Sort Lab
Sorting
Problem shape
Beginner
4
8
15
16
23
42
Core idea
Select the smallest remaining value and place it at the current position.
Step 1 / 5
Problem shape
Sorting Beginner
Selection Sort
Welcome to the Selection Sort algorithm lab. Select the smallest remaining value and place it at the current position. Use it when: Use for teaching or when swaps are very expensive and n is tiny. The time complexity is O(n^2), and the space complexity is O(1). The pseudocode is: for i from 0 to n - 1 best = i for j from i + 1 to n - 1 if a[j] < a[best] best = j swap a[i], a[best] As you practice, track the state that changes and the condition that makes the algorithm stop.
Time
O(n^2)
Space
O(1)
Pseudocode
for i from 0 to n - 1
best = i
for j from i + 1 to n - 1
if a[j] < a[best] best = j
swap a[i], a[best] 1.0x