Sorting Algorithms
Comparing Sorting Algorithms
Comparing Sorting Algorithms means deciding which implementation meets the requirements of a particular task. A list of numbers may need only ascending order, while registration records may also need to retain arrival order among equal priorities. Your decision therefore involves more than speed: compare the required ordering, the extra memory available, and the performance guarantee the application needs. Focus on these properties rather than the internal procedures of individual algorithms.
Equal keys and stability
A record's sort key might be a score, date, name, or priority. Other fields travel with the record but do not necessarily participate in comparisons.
Stability is the property of preserving the original relative order of records whose sort keys compare equal. If Ana appears before Chen and both have priority 2, a stable ascending sort keeps Ana before Chen. Their positions can change; their order relative to each other cannot.
An unstable implementation may preserve a particular pair by chance. Its classification means that preservation is not guaranteed for every input. Stability matters when the existing order carries information, such as arrival time. It usually makes no observable difference when equal values have no associated information.
Extra memory and in-place sorting
An in-place sort uses only a constant amount of auxiliary storage beyond the input under the strict definition used here. Auxiliary storage includes temporary arrays and active function-call storage. Overwriting the original array does not, by itself, establish that a sort is in place.
For example, an implementation that rearranges the input array but allocates a second array of n records uses space proportional to n. An implementation using ten temporary variables regardless of n uses constant auxiliary space.
Some sources use in-place more loosely for implementations with a small recursive call stack. When comparing implementations, read the stated space bound and what it includes. Distinguish auxiliary space from total memory: the latter also includes the input itself.
Time bounds and practical comparisons
Big-O notation describes growth, not an exact execution time. A bound of O(nlogn) does not specify a number of milliseconds, and an O(n2) upper bound alone does not prove that growth is quadratic.
For explicit time models, a useful comparison is
where n is the number of records and a,b are positive constants for the stated implementations and measurement conditions. Doubling n multiplies the quadratic term by four. The other term increases by
Larger constants can still make the better growth class slower on small inputs. Comparisons may also depend on the cost of moving records or comparing keys.
These common implementations illustrate why you compare several properties together.
Implementation | Conventional worst-case time bound | Auxiliary space, including calls | Stable? |
|---|---|---|---|
Standard insertion sort | O(n2) | O(1) | Yes, if equal keys are not moved past each other |
Standard array merge sort | O(nlogn) | O(n) | Yes, if merging preserves ties |
Conventional recursive quicksort | O(n2) | O(n) in the worst case | No |
Standard array heapsort | O(nlogn) | O(1) | No |
These are implementation properties, not universal promises attached to names. Best-case time describes the most favourable inputs of a given size. Worst-case time bounds every input of that size. Average-case time requires a specified input distribution or randomization model; it is not automatically a deadline guarantee.
What to remember
- Check whether equal-key records must retain their original order.
- Count auxiliary memory separately from input storage.
- Compare time bounds for the same case and assumptions.
- Use explicit models or measurements for numerical predictions; growth classes alone do not give exact times.
Example: Choosing a registration sorter
A fictional event system holds 2048 registration records in arrival order. It must sort them by ticket category while retaining arrival order within each category. The system permits at most 400000 operations according to the documented worst-case bounds and at most 40 KiB of auxiliary storage. One KiB equals 1024 bytes; input storage is budgeted separately.
Here, auxiliary space means storage used beyond the input records. The following requirements include all temporary storage and active function calls. Treat the operation bounds as exact numerical expressions for this planning decision, rather than predictions of elapsed time.
Implementation | Stable | Worst-case operation bound | Auxiliary storage |
|---|---|---|---|
P | Yes | 4n2 | 64 bytes |
Q | Yes | 12nlog2n | 16n bytes |
R | No | 8nlog2n | 128 bytes |
Example: Compare the implementations for the current registration batch, then recommend one that meets every requirement.
a) Calculate each implementation's worst-case operation bound for 2048 records.
First evaluate the shared intermediate quantities. Because 2048=211, its base-two logarithm is ________. The square of the input size is ________.
Substitute these values into the documented bounds:
The bounds are 16777216 operations for P, 270336 for Q, and 180224 for R. Q and R meet the 400000-operation policy; P does not.
b) Determine whether each implementation fits the auxiliary-memory limit.
Convert the memory limit to bytes before comparing it with the table. The 40 KiB limit is ________, since
P requires 64 bytes and R requires 128 bytes, both below the limit. For Q, multiply the number of records by its auxiliary bytes per record. The intermediate allocation is ________:
Converting Q's requirement gives
All three implementations fit the memory limit. In particular, Q requires 32 KiB, below the available 40 KiB. Input storage is excluded because this limit specifically concerns auxiliary memory.
c) Recommend an implementation that meets the complete specification.
The specification requires arrival order to be preserved within each ticket category. That is a stability requirement because category is the sort key and arrival order is the existing order among equal keys.
P is stable and fits memory, but its operation bound exceeds the policy limit. R fits both numerical limits, but its unstable classification does not guarantee the required equal-category ordering. Q is stable, has an operation bound of 270336, and uses 32 KiB of auxiliary memory.
Choose Q. It is the only listed implementation that satisfies all three requirements. R's smaller operation bound does not make it acceptable when its ordering guarantee fails the specification.
A library sorts borrowing records by their due dates. Match each sorting term to its meaning when describing the task and checking the output.
Rule used to determine the relative order of two values
Arrangement in which each value is less than or equal to the next value
Rearrangement preserving every complete input record and its number of occurrences
Field whose values determine the order of records
Comparator
Permutation
Nondecreasing order
Sort key
A benchmark planning model assigns implementation A exactly n2 operations and implementation B exactly nlog2n operations for sorting n records. Complete the table with exact integer operation counts, using the models rather than measured execution times.
| Number of records | A operations | B operations |
| 128 | ||
| 256 |