You just sorted an array by hand — and it took you more swaps than par. Par was 3. You probably used 5 or 6. That gap isn't about skill; it's about information. The par player knew something you didn't.
Here's the question: what makes par possible?
Look at this tiny array:
// index: 0 1 2const arr = [3, 1, 2];Value 3 is sitting at index 0. But in a sorted array, where
should 3 live? And where should 1 and 2 end up?
What if every value already knows its home — and you just need to read the address? Below, you'll place each value where it belongs. See if you can spot the pattern before we spell it out.
Here's the formula you just discovered:
const homeIndex = value - 1;That's it. Every value in an array of [1, n] carries its own
address. Think of the array as a mailroom and each value as a letter
with the destination printed right on it — the data is the
routing instruction. You don't need to compare values to find where
they go. You just read the label.
Here's every value from [2, 5, 1, 4, 3] and its home:
| value | homeIndex (value - 1) |
|---|---|
| 2 | 1 |
| 5 | 4 |
| 1 | 0 |
| 4 | 3 |
| 3 | 2 |
When you traced the arrows earlier, you saw something interesting:
the mappings formed loops. Value 2 points to index 1, which
holds 5, which points to index 4, which holds 3, which points
to index 2, which holds 1, which points back to index 0. Not
one big loop — several smaller cycles.
Those cycles aren't a coincidence. They're about to become the engine of the entire algorithm. Next up: what happens when you follow a cycle all the way around?