8.4. Sorting¶
Another common operation computers need to do with data is to sort. What are the top 10 results on a web search the term “sort algorithm”?, Rank the states of the US based on population, Print an alphabetical class roster - these are all examples of tasks that require putting data in some kind of order. Sometimes we may not care what order our data is in, but want to use an algorithm like binary search that only works on ordered data.
Much like how we can use different strategies for searching a list for data, there are many possible algorithms we can use to sort a list of data. Each algorithm uses a different strategy to rearrange the data - these different strategies mean the algorithms behave differently. Some algorithms will be faster than others, maybe all the time, maybe just in certain situations. Some algorithms will produce useful results if we stop them early - like finding the smallest 5 things without completely sorting the list.
In the next few sections will learn a few sort algorithms so we can compare various strategies.