How does sorting work
You've probably lined up by height before. Sorting means putting jumbled things in order. The secret is simple: compare heights with the person next to you and send the taller one back. Repeat this, and the line comes out right.
What is sorting?
Ask people to line up by height, and at first they're all jumbled.
Sorting puts them in order from smallest to biggest.
Below, you can compare the jumbled line with the sorted one.
Jumbled vs sorted. Jumbled → smallest to biggest.
A sorted line looks nice.
But more important, once things are in order, they're easier to find later.
A dictionary is in alphabetical order, which is why you can find a word quickly.
Measuring against your neighbor
So how do you line things up?
The secret is very simple: look at two neighbors, and if the left one is bigger, swap them.
Smaller goes forward, bigger goes back. That one move is all there is.
If the left one is bigger, swap them. Compare the two → the bigger one goes back.
You only ever compare two.
Two at a time: swap if the left is bigger, leave them if not.
No complicated thinking, just repeating this simple move over and over.
The big ones move back
Now let's really sort.
Starting from the left, compare neighbors and push the bigger one back. After one pass, the biggest one settles at the very end.
Repeat, and the line falls into place step by step.
Press "start sorting" to watch it happen.
Compare and swap, over and over. Yellow=comparing · red=swapping · green=in place.
You can see the big bars float to the back like bubbles.
Each pass, one more finds its place.
Simple comparisons and swaps add up to a complete line.
Why does it always work?
Every pass, the biggest remaining one settles at the back.
After two passes the second biggest is in place, after three the third.
Since one more fills in from the back with each step, enough steps will get them all in place.
Each press of "one more step" puts one more in place, and enough steps complete the line.
That's why sorting is reliable.
However jumbled things are, repeating the steps always gets the line right.
Fast or slow, if you keep to the order, the ending is certain.
Why sorting is useful
Sorting is everywhere.
Ranking by score, listing names alphabetically, showing products by price.
Above all, a sorted line is much easier to search. That's a story coming a little later.
Now that things are lined up, what's next. Sorting = putting things in order → searching · ranking · beyond.
Lining things up is useful on its own, but it's also preparation for what comes next.
Once things are tidy, finding and choosing get easier.
The power of order is bigger than you'd think.