Criteria for Choosing a Sorting Algorithm
To choose a sorting algorithm, consider the qualitative criteria in Table 4-6. These may help your initial decision, but you likely will need more quantitative measures to guide you.
Table 4-6. Criteria for choosing a sorting algorithm
|
Criteria |
Sorting algorithm |
|---|---|
|
Only a few items |
Insertion Sort |
|
Items are mostly sorted already |
Insertion Sort |
|
Concerned about worst-case scenarios |
Heap Sort |
|
Interested in a good average-case result |
Quicksort |
|
Items are drawn from a dense universe |
Bucket Sort |
|
Desire to write as little code as possible |
Insertion Sort |
To choose the appropriate algorithm for different data, you need to know some properties about your input data. We created several benchmark data sets on which to show how the algorithms presented in this chapter compare with one another. Note that the actual values of the generated tables are less important because they reflect the specific hardware on which the benchmarks were run. Instead, you should pay attention to the relative performance of the algorithms on the corresponding data sets:
- Random strings
Throughout this chapter, we have demonstrated performance of sorting algorithms when sorting 26-character strings that are permutations of the letters in the alphabet. Given there are n! such strings, or roughly 4.03*1026 strings, there are few duplicate strings in our sample data sets. In addition, the cost of comparing elements is not constant, because of the occasional ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access