javac SortingPatterns.java && ./run --mode=interview-prep
Sorting Algorithm Patterns
Visualize classic sorting algorithms with step-by-step animations. Watch comparisons, partitions, merges, and counts transform the array into sorted order — one operation at a time.
01_selection_sort.java
Selection Sort keeps choosing the minimum item from the unsorted tail and placing it at the front. It is simple and easy to reason about, but it does a full scan on every pass, so it is O(n²).
General Definition
A simple in-place sorting strategy that repeatedly finds the smallest remaining value and swaps it into its correct position.
Professional Definition — say this in an interview
Selection Sort minimizes the number of swaps by fixing one final position per pass: it scans the unsorted section to find the minimum, swaps it with the current index, and repeats until the array is sorted. This makes it easy to teach, but it is not efficient for large inputs because each pass still scans the rest of the array.
Interview keywords — if you hear these, think of this pattern
minimum selectionin-place sortO(n²)simple sortingswap smallest into place
SOURCE
Java
public void selectionSort(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { int minIdx = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIdx]) minIdx = j; } int tmp = arr[i]; arr[i] = arr[minIdx]; arr[minIdx] = tmp; } }
DEBUGGER — live watch0 / 15
64
0
25
1
12
2
22
3
11
4
Not started
DEBUG STEPS0
Press ▶ Play or Next to start stepping through the algorithm.
Practice these — same pattern, different skin