LearnWithKiriti

Selection Sort

Easy GitHub ↗
đź’» Try it yourself first. Write your solution in Java, C++, Python or JavaScript and run it against hidden tests, right here. New: mock interview with Mr. Tock. Explain your approach out loud, he pushes you to improve it, then you code it. You get a scorecard at the end.
Play
On this page
  1. Revision notes
  2. Optimal: Selection Sort

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

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).

SelectionSortOptimal.java
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;
    }
}