Skip to content

11.1   Sorting algorithms

A sorting algorithm arranges data in a specified order. Sorting is widely used because ordered data is usually easier to search, analyze, and process efficiently.

As Figure 11-1 shows, we can sort integers, floating-point numbers, characters, strings, and other types of data. We choose the ordering rule to suit the task, whether it uses numeric value, ASCII character codes, or a custom rule.

Figure 11-1   Examples of data types and ordering rules

Description of Figure 11-1: Ordering rules for different data types

Three rows compare inputs and outputs for integers sorted in ascending numeric order, English names in lexicographic order, and grade strings in a custom order from D to A+.

Each row begins with unsorted input on the left. A right-pointing arrow labeled with the ordering rule leads to the sorted result on the right. The diagram compares inputs and outputs without showing the internal operations.

Names are compared character by character in lexicographic order. The five names here begin with different letters, so their first letters determine the order. Grades follow the custom order D < C < B- < B < B+ < A < A+, from lowest to highest, rather than ordinary ASCII lexicographic order. B-, B, and B+ are three distinct grades. The table lists all the data in the same top-to-bottom row order as the figure.

Ordering rules and data before and after sorting for all three rows
Figure row and data typeRule on the arrowUnsorted inputSorted result
Row 1: 8 integersSort integers by numeric value[7, 3, 2, 6, 0, 1, 5, 4][0, 1, 2, 3, 4, 5, 6, 7]
Row 2: 5 English namesSort strings in lexicographic order[Kobe, Durant, Curry, Lebron, Harden][Curry, Durant, Harden, Kobe, Lebron]
Row 3: 7 gradesSort strings by a custom rule[B-, A+, B, B+, A, D, C][D, C, B-, B, B+, A, A+]

Sorting changes only the order of the elements. The chosen comparison rule determines the output.

11.1.1   Criteria for comparing sorting algorithms

Running time. We want low time complexity and a small total number of operations (a small constant factor in the running time). Speed is especially important for large datasets.

In-place operation. An in-place sort works directly on the original array, saving memory by avoiding an auxiliary array. In-place sorts usually move data less and run faster.

Stability. A stable sort preserves the relative order of equal elements in the array.

Stability is essential when sorting successively by multiple keys. Suppose a table of students contains names in the first column and ages in the second. An unstable sort can discard ordering information from the input:

# The input is sorted by name
# (name, age)
  ('A', 19)
  ('B', 18)
  ('C', 21)
  ('D', 19)
  ('E', 23)

# Suppose we sort the list by age using an unstable algorithm
# The relative order of ('D', 19) and ('A', 19) changes
# Students of the same age are no longer sorted by name
  ('B', 18)
  ('D', 19)
  ('A', 19)
  ('C', 21)
  ('E', 23)

Adaptivity. An adaptive sort takes advantage of existing order in the input to do less work and run faster. Its best-case time complexity is usually better than its average time complexity.

Use of comparisons. A comparison sort uses comparison operators (\(<\), \(=\), \(>\)) to establish the relative order of elements and sort the array. Its worst-case time complexity has a lower bound of \(\Omega(n \log n)\). A non-comparison sort does not rely on these comparisons and can run in \(O(n)\) time, but it applies to a narrower range of inputs.

11.1.2   The ideal sorting algorithm

Fast, in-place, stable, adaptive, and broadly applicable. So far, no known sorting algorithm has all these properties. The choice therefore depends on the data and the requirements of the problem.

In the following sections, we will study several sorting algorithms and use these criteria to analyze their strengths and weaknesses.

Share your thoughts, questions, or suggestions in the comments.