Origins and Practical Significance of Sorting
Sorting is among the earliest math/computing problems people faced. From ancient ledgers to modern databases and search engines, sorting is everywhere.
- Ancient ledgers and rosters:
- In Egypt, Babylon, and other civilizations, officials ordered goods, population, and taxes for lookup and management.
- Imperial exam rankings and official promotions in ancient China also required ordering.
- Libraries and dictionaries:
- Librarians shelved books by number, author, or subject to ease borrowing.
- Dictionaries rely on alphabet/pinyin ordering rules.
- Modern applications:
- Search results, product recommendations, ranking lists, parcel sorting—all powered by efficient sorting algorithms.
- Sorting: the process of arranging a group of data elements in ascending or descending order of the key
- Key: the field the ordering is based on; it can be a primary key (unique) or a secondary key
Classification of Sorting Algorithms
- By storage medium: internal sorting (all the data fits in memory, the focus of this chapter) and external sorting (the data is too large, so external storage has to be used)
- By comparison method: comparison-based sorting (the order is decided by comparing the keys) and non-comparison sorting (radix, counting, and bucket sorting, which never compare the keys)
Stability
A stable sort keeps the relative order of two equal elements unchanged. Stability matters a great deal in multi-key sorting, for example sorting first by the total score and then by the score of one particular subject.
Criteria for Evaluating a Sorting Algorithm
- Time complexity: the number of comparisons and moves in the best, worst, and average cases
- Space complexity: the auxiliary space the sort needs
- Stability: whether the relative order of equal elements is preserved
- Simplicity of the implementation: the constant factor and the complexity of the code
- Improves lookup efficiency: sorted data enables binary search and range queries.
- Data analysis and presentation: ordered data is easier to analyze, compare, and visualize.
- System optimization: many algorithms (merge, dedup, clustering, etc.) build on sorting.
Sorting is a foundation of computer science and a key tool for managing and understanding the world.
