Sorting Algorithms: Sorting Methods
Sorting Algorithms: Sorting Methods
We live in a world filled with comparisons. Whether you're in the checkout line trying to pick out a pack of gum (EZ Chew has better flavor, but UChew donates a song from iTunes to an orphan for every purchase) or figuring out which bubble popping app to add to your phone, the most common way to decide is by making comparisons.
…Or picking randomly. That works too.
If you're comparing fruit, a comparison sort would be the equivalent of us taking a apple in one hand, an orange in the other, deciding which one's better, and moving them based on that decision. But what if you decide the apple's better and then have to compare it to another orange. Is it just that apple, or are all apples better than all oranges?
Sometimes you just can't compare apples to oranges.
In non-comparison sorts, the algorithm uses the value of each thing to "group" items together. So instead of comparing apples to oranges, we'd group all the oranges together and all the apples together without making a single comparison. Boom: sorted.
We're sure there's a point to sorting fruit, but it seems a little cruel to us.
We can also sort three digit numbers without comparing them. Take these numbers:
546 | 322 | 357 |
We can start by grouping together values in the ones place, so that now the list is in ascending order (if we cover up the tens and hundreds places): 2, 6, 7
322 | 546 | 357 |
Then we group by the tens place so that the list is ordered if we forget the hundreds: 22, 46, 57.
322 | 546 | 357 |
After all that, if we group the hundreds place, everything's sorted.
322 | 357 | 546 |
Boom.
Sorting by grouping (and all other forms of sorting that don't include comparisons, for that matter) are known as—wait for it—non-comparison sorts. Numbers and fruit aren't really compared so much as grouped together, making this type of algorithm a little bit more efficient than other algorithms.
Sometimes.