AlgoViz
← Back to trail

What is Sorted?

Easy+15 XP

'Sorted' isn't just 'tidy' — it's a promise: each box is never smaller than the one before it.

No visualization loaded.

Watch

i

Press Run to begin.

Sorted means the numbers are lined up so they never go down as you read left to right — each box is bigger than (or equal to) the one before it. It sounds simple, but that one promise is what lets a computer take giant shortcuts, like jumping to the middle of the array and instantly ruling out half of it.

Does sorted have to mean smallest-to-biggest?

That's the most common kind (ascending order). You can also sort biggest-to-smallest (descending), or sort words A-to-Z. The key idea is the same: there's a consistent rule, and every neighbouring pair obeys it. In this course, 'sorted' means smallest-to-biggest unless we say otherwise.

Why does being sorted make searching so much faster?

Because order lets you rule things out without looking. If you peek at the middle box and it's too big, every box to its right is also too big — you can ignore all of them at once. In an unsorted array you can't assume anything, so you're stuck opening boxes one by one.

How do I check if an array is sorted?

Walk through it and compare each box with the one right after it. If you ever find a box that's bigger than its right neighbour, it's not sorted. If you get all the way through with no such pair, it is. That's exactly what the animation does.

If sorting is so useful, why isn't everything just always sorted?

Because sorting takes work — you have to rearrange the boxes first, which costs time. It's worth it when you'll search the same data many times. If you only look once, sorting first might cost more than it saves. That trade-off comes up again and again.

🧠Sorted = never goes down, left to right. That single promise is what binary search and two pointers stand on — break it and they break.
pair being comparedout-of-order boxfinal sorted order