Write insertion and selection sort and know when they are the right choice.
The front of the list is always sorted: selection adds the smallest remaining value to it, and insertion adds the next value, shifting larger ones aside.
Use it when the list is short, or nearly sorted, or a write is costly. Signal words: the list is almost in order, each item is only a few places out, the fewest writes, a short list.
Start: the front of the list is empty, or the first value alone. It is sorted.
Step: selection finds the smallest of the rest and swaps it to the next place. Insertion takes the next value and shifts larger values right until it fits.
Stop: when the front covers the list. Selection stops after n − 1 passes, insertion after n − 1 insertions.
It does not suit a long random list: both sorts are O(n²) there, and the comparison floor is about n log n.
Cost: selection makes n(n − 1)/2 comparisons and at most n − 1 swaps. Insertion makes n − 1 comparisons plus about one for each shift, and the shifts equal the inversions: O(n + inversions).
def selection_sort(values):
# [1] Work on a copy; the front part of it is always final.
items = list(values)
n = len(items)
# [2] For each place, find the smallest value among the places that are not final yet.
for i in range(n - 1):
smallest = i
for j in range(i + 1, n):
if items[j] < items[smallest]:
smallest = j
# [3] Swap it into place i: at most one swap for each place.
if smallest != i:
items[i], items[smallest] = items[smallest], items[i]
# [4] Every place is final: return the sorted list.
return itemspublic int[] selectionSort(int[] values) {
// [1] Work on a copy; the front part of it is always final.
int[] items = values.clone();
int n = items.length;
// [2] For each place, find the smallest value among the places that are not final yet.
for (int i = 0; i < n - 1; i++) {
int smallest = i;
for (int j = i + 1; j < n; j++) {
if (items[j] < items[smallest]) {
smallest = j;
}
}
// [3] Swap it into place i: at most one swap for each place.
if (smallest != i) {
int tmp = items[i];
items[i] = items[smallest];
items[smallest] = tmp;
}
}
// [4] Every place is final: return the sorted list.
return items;
}vector<int> selectionSort(vector<int>& values) {
// [1] Work on a copy; the front part of it is always final.
vector<int> items = values;
int n = (int)items.size();
// [2] For each place, find the smallest value among the places that are not final yet.
for (int i = 0; i < n - 1; i++) {
int smallest = i;
for (int j = i + 1; j < n; j++) {
if (items[j] < items[smallest]) {
smallest = j;
}
}
// [3] Swap it into place i: at most one swap for each place.
if (smallest != i) {
swap(items[i], items[smallest]);
}
}
// [4] Every place is final: return the sorted list.
return items;
}function selectionSort(values) {
// [1] Work on a copy; the front part of it is always final.
const items = [...values];
const n = items.length;
// [2] For each place, find the smallest value among the places that are not final yet.
for (let i = 0; i < n - 1; i++) {
let smallest = i;
for (let j = i + 1; j < n; j++) {
if (items[j] < items[smallest]) {
smallest = j;
}
}
// [3] Swap it into place i: at most one swap for each place.
if (smallest !== i) {
[items[i], items[smallest]] = [items[smallest], items[i]];
}
}
// [4] Every place is final: return the sorted list.
return items;
}def insertion_sort(values):
# [1] Work on a copy; the front part of it is always in order.
items = list(values)
# [2] Take each value in turn.
for i in range(1, len(items)):
x = items[i]
j = i - 1
# [3] Shift every larger value one place right. Equal values stay where they are.
while j >= 0 and items[j] > x:
items[j + 1] = items[j]
j -= 1
# [4] Drop the value into the gap.
items[j + 1] = x
# [5] The whole list is in order: return it.
return itemspublic int[] insertionSort(int[] values) {
// [1] Work on a copy; the front part of it is always in order.
int[] items = values.clone();
// [2] Take each value in turn.
for (int i = 1; i < items.length; i++) {
int x = items[i];
int j = i - 1;
// [3] Shift every larger value one place right. Equal values stay where they are.
while (j >= 0 && items[j] > x) {
items[j + 1] = items[j];
j--;
}
// [4] Drop the value into the gap.
items[j + 1] = x;
}
// [5] The whole list is in order: return it.
return items;
}vector<int> insertionSort(vector<int>& values) {
// [1] Work on a copy; the front part of it is always in order.
vector<int> items = values;
// [2] Take each value in turn.
for (int i = 1; i < (int)items.size(); i++) {
int x = items[i];
int j = i - 1;
// [3] Shift every larger value one place right. Equal values stay where they are.
while (j >= 0 && items[j] > x) {
items[j + 1] = items[j];
j--;
}
// [4] Drop the value into the gap.
items[j + 1] = x;
}
// [5] The whole list is in order: return it.
return items;
}function insertionSort(values) {
// [1] Work on a copy; the front part of it is always in order.
const items = [...values];
// [2] Take each value in turn.
for (let i = 1; i < items.length; i++) {
const x = items[i];
let j = i - 1;
// [3] Shift every larger value one place right. Equal values stay where they are.
while (j >= 0 && items[j] > x) {
items[j + 1] = items[j];
j--;
}
// [4] Drop the value into the gap.
items[j + 1] = x;
}
// [5] The whole list is in order: return it.
return items;
}This version writes items[j] >= x in the shifting loop. A value then moves in front of an equal value, and equal values change order.
This version writes while j > 0 and items[j] > x, as if place 0 needed no check. A value that belongs before place 0 never passes it.
This version swaps items[i] with items[j] every time it finds a smaller value, instead of remembering the place and swapping once. It sorts correctly, but it is not selection sort, and it loses the limit of n − 1 swaps.
This version uses selection sort for the night list because "simple sorts are fine for simple data". The data is almost sorted, but selection does not care.
The app also sorts 10,000 random bookings. The list has no order to use. Insertion makes 24,825,638 comparisons and 24,815,649 shifts on one seeded random list of values from 1 to 1,000,000.
systemdesignmastery.dev/dsa/cheatsheets/simple-sorts