Sorting Algorithms· 15 min

Selection Sort

Find the minimum and place it at the front

Setup

Size20
Step 1 / 323

Current step

1 / 323

Action: Starting Selection Sort. We'll find the minimum in the unsorted portion and place it at the front.

Why: This is the next operation required by the algorithm.

Result: The visualization now reflects this operation.

Step log

18
9
36
17
25
6
29
11
53
59
51
32
40
52
37
9
12
57
55
53
012345678910111213141516171819
Unsorted
Comparing
Swap needed
Swapping
Moving
Pivot
Sorted
selection.java
1void selectionSort(int[] arr) {
2 int n = arr.length;
3 for (int i = 0; i < n - 1; i++) {
4 int minIdx = i;
5 for (int j = i + 1; j < n; j++) {
6 if (arr[j] < arr[minIdx]) minIdx = j;
7 }
8 int t = arr[i]; arr[i] = arr[minIdx]; arr[minIdx] = t;
9 }
10}
● Java

Selection Sort

Divides the array into a sorted prefix and an unsorted suffix, repeatedly selecting the minimum from the unsorted part and moving it to the front.

Time complexity
O(n²)
Space
O(1)
Stable
No
Comparisons
190
n = 201 / 323 steps