Selection Sort
đź’» Try it yourself first. Write your solution in Java, C++, Python or JavaScript and run it against hidden tests, right here.On this page
Revision notes
Selection Sort
Video: “Selection Sort: fewest moves wins” on LearnWithKiriti (Kodee).
Sort an array from smallest to largest by repeatedly selecting the smallest remaining number and swapping it to the front.
Example: [7, 2, 8, 1, 5] -> [1, 2, 5, 7, 8]
| Folder | Approach | Time | Extra space |
|---|---|---|---|
Optimal-SelectionSort/ |
Selection sort: For each frontSlot, check every candidate in the rest, remember the smallest (smallestSpot), and swap it in. At most n - 1 swaps: the fewest moves of the simple sorts. | O(n²) | O(1) |
Rebuild it from the name
Selection: select the smallest, put it at the front. frontSlot is the spot that gets the next smallest number. Check every candidate in the rest and remember where the smallest one sits (smallestSpot), then swap it into frontSlot. The sorted part grows by one from the left. One swap per pass at most, so it makes the fewest writes of the simple sorts, useful when writing is expensive.
See Optimal-SelectionSort/pseudocode.md. Every program runs Example 1 and prints the answer (the part under “Run it locally”); the solution itself is the same code as the practice panel’s Start menu on learnwithkiriti.com, where Shelf Bot can act it out.
Optimal: Selection Sort
Selection sort
For each frontSlot, check every candidate in the rest, remember the smallest (smallestSpot), and swap it into frontSlot. At most n - 1 swaps: the fewest moves of the simple sorts.
for frontSlot from 0 to size - 2:
smallestSpot = frontSlot
for candidate from frontSlot + 1 to size - 1:
if nums[candidate] < nums[smallestSpot]:
smallestSpot = candidate
if smallestSpot != frontSlot:
swap nums[frontSlot] and nums[smallestSpot]
Dry run on Example 1:
| frontSlot | smallestSpot (value) | array after the swap |
|---|---|---|
| 0 | 3 (1) | [1, 2, 8, 7, 5] |
| 1 | 1 (2) | [1, 2, 8, 7, 5] (already in place, no swap) |
| 2 | 4 (5) | [1, 2, 5, 7, 8] |
| 3 | 3 (7) | [1, 2, 5, 7, 8] |
Always about n²/2 comparisons: O(n²), even on sorted input. At most n - 1 swaps. O(1) extra space. Not stable (a swap can jump over an equal number).
class Solution {
public void selectionSort(int[] nums) {
// each pass selects the smallest of the rest and puts it at frontSlot
for (int frontSlot = 0; frontSlot < nums.length - 1; frontSlot++) {
int smallestSpot = frontSlot;
for (int candidate = frontSlot + 1; candidate < nums.length; candidate++) {
if (nums[candidate] < nums[smallestSpot]) {
smallestSpot = candidate;
}
}
if (smallestSpot != frontSlot) {
swap(nums, frontSlot, smallestSpot);
}
}
}
private void swap(int[] nums, int left, int right) {
int held = nums[left];
nums[left] = nums[right];
nums[right] = held;
}
}class Solution {
public:
void selectionSort(vector<int>& nums) {
// each pass selects the smallest of the rest and puts it at frontSlot
for (int frontSlot = 0; frontSlot < (int)nums.size() - 1; frontSlot++) {
int smallestSpot = frontSlot;
for (int candidate = frontSlot + 1; candidate < (int)nums.size(); candidate++) {
if (nums[candidate] < nums[smallestSpot]) {
smallestSpot = candidate;
}
}
if (smallestSpot != frontSlot) {
swap(nums[frontSlot], nums[smallestSpot]);
}
}
}
};def selection_sort(nums):
# each pass selects the smallest of the rest and puts it at front_slot
for front_slot in range(len(nums) - 1):
smallest_spot = front_slot
for candidate in range(front_slot + 1, len(nums)):
if nums[candidate] < nums[smallest_spot]:
smallest_spot = candidate
if smallest_spot != front_slot:
nums[front_slot], nums[smallest_spot] = nums[smallest_spot], nums[front_slot]function selectionSort(nums) {
// each pass selects the smallest of the rest and puts it at frontSlot
for (let frontSlot = 0; frontSlot < nums.length - 1; frontSlot++) {
let smallestSpot = frontSlot;
for (let candidate = frontSlot + 1; candidate < nums.length; candidate++) {
if (nums[candidate] < nums[smallestSpot]) {
smallestSpot = candidate;
}
}
if (smallestSpot !== frontSlot) {
[nums[frontSlot], nums[smallestSpot]] = [nums[smallestSpot], nums[frontSlot]];
}
}
}