seegongsik
Algorithms

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.

01

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.

3
1
4
2
5
Heights are all mixed up. Making this into order is sorting.

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.

02

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.

3
1
4
2
5
Compare two bars. If the left is bigger, swap, and the bigger goes back.

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.

03

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.

3
1
4
2
5
Yellow is comparing, red is swapping, green is settled.

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.

04

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.

3
1
4
2
5
Not started yet
Each round (one pass) adds one green. The line falls into order from the back.

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.

05

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.

Sorting = ordering in a line
Compare neighbors · bigger ones back · repeat and the line is in order.
Searching
Finding what you want
Ranking
Ordering by score or price
Beyond
Faster ways to sort
Next we'll look at faster ways to sort, and after that at "searching," quickly finding what you want in a sorted line.

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.

In one lineSorting puts jumbled things in order. Repeatedly compare neighbors and send the bigger one back, and the line falls into place step by step. Since one more finds its place with each step, the line ends up right no matter how jumbled it was, and once things are tidy, they're easier to find.
← Algorithms
Was this helpful? Support seegongsik