Lesson 1 · Unit 02
Swap your way up
Bubble sort and selection sort, one pass at a time
What you practise
- One bubble pass
- Selection picks the minimum
- O(n²) comparisons
Try a question from this lesson
Pick one
Walk [5, 2, 8, 1] left to right, comparing each pair of neighbours and swapping them if the left one is bigger. What is the list after one walk?
Correct answer
[2, 5, 1, 8]
Why
5 and 2 swap, 5 and 8 stay, 8 and 1 swap: [2, 5, 1, 8]. One pass does not sort the list, but the largest value, 8, has bubbled to the end for good.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Bubble sort
Bubble sort walks the list comparing neighbours, swaps each pair that is out of order, and repeats that pass until the list is sorted.
After the first pass the largest value is in its final place, after the second the two largest, and so on. So each pass can stop one index earlier: that is what end counts down.
def bubble_sort(a):
n = len(a)
for end in range(n - 1, 0, -1):
for i in range(end):
j = i + 1
if a[i] > a[j]:
a[i], a[j] = a[j], a[i]
return afunction bubbleSort(a) {
const n = a.length;
for (let end = n - 1; end > 0; end--) {
for (let i = 0; i < end; i++) {
const j = i + 1;
if (a[i] > a[j]) {
[a[i], a[j]] = [a[j], a[i]];
}
}
}
return a;
}function bubbleSort(a: number[]) {
const n = a.length;
for (let end = n - 1; end > 0; end--) {
for (let i = 0; i < end; i++) {
const j = i + 1;
if (a[i] > a[j]) {
[a[i], a[j]] = [a[j], a[i]];
}
}
}
return a;
}Python notea[i], a[j] = a[j], a[i] swaps two items in one line: the right side is built first, then unpacked.
JavaScript note[a[i], a[j]] = [a[j], a[i]] is a destructuring swap: no temporary variable needed.
TypeScript notea: number[] types the parameter. The return type is inferred as number[] from return a, so it is left out.
Concept
Selection sort
A different plan: find the smallest value in the unsorted part and swap it to the front of that part. Then do the same for the rest.
low remembers the index of the smallest value seen so far in this scan. There is one swap per scan, however long the scan is.
def selection_sort(a):
n = len(a)
for i in range(n - 1):
low = i
for j in range(i + 1, n):
if a[j] < a[low]:
low = j
a[i], a[low] = a[low], a[i]
return afunction selectionSort(a) {
const n = a.length;
for (let i = 0; i < n - 1; i++) {
let low = i;
for (let j = i + 1; j < n; j++) {
if (a[j] < a[low]) low = j;
}
[a[i], a[low]] = [a[low], a[i]];
}
return a;
}function selectionSort(a: number[]) {
const n = a.length;
for (let i = 0; i < n - 1; i++) {
let low = i;
for (let j = i + 1; j < n; j++) {
if (a[j] < a[low]) low = j;
}
[a[i], a[low]] = [a[low], a[i]];
}
return a;
}TypeScript notelet low = i is inferred as number. It is let, not const, because the inner loop reassigns it.
9 more exercises in the app
- 2× type the answer
- 4× multiple choice
- 1× fill the blanks
- 1× true or false
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.