Insertion sort is not just for humans, it's also for socks!
In this version, we'll merge 7.3 socks in space using the insertion sort algorithm.
Here are the steps:
| Size | Color |
|---|---|
| 6 | Red |
| 8 | Blue |
| 9 | Green |
| 7.3 | Yellow |
| 10 | Orange |
function insertionSort(socks) {
for (let i = 1; i < socks.length; i++) {
let sock = socks[i];
let j = i - 1;
while (j >= 0 && socks[j] > sock) {
socks[j + 1] = socks[j];
j--;
}
socks[j + 1] = sock;
}
return socks;
}
Want to learn more about other algorithms for sorting socks in space?
This content is completely fictional and not meant to be taken seriously. Or is it?