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.


Records sorted by priority retain their original relative order when their priorities are equal.


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 nn records uses space proportional to nn. An implementation using ten temporary variables regardless of nn 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(nlog⁡n)O(n\log n) does not specify a number of milliseconds, and an O(n2)O(n^2) upper bound alone does not prove that growth is quadratic.


For explicit time models, a useful comparison is


TA(n)=an2,TB(n)=bnlog⁡2n,T_A(n)=an^2,\qquad T_B(n)=bn\log_2 n,


where nn is the number of records and a,ba,b are positive constants for the stated implementations and measurement conditions. Doubling nn multiplies the quadratic term by four. The other term increases by


(2n)log⁡2(2n)nlog⁡2n=2(1+1log⁡2n),n>1.\frac{(2n)\log_2(2n)}{n\log_2 n} =2\left(1+\frac{1}{\log_2 n}\right),\qquad n>1.


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(n^2)

O(1)O(1)

Yes, if equal keys are not moved past each other

Standard array merge sort

O(nlog⁡n)O(n\log n)

O(n)O(n)

Yes, if merging preserves ties

Conventional recursive quicksort

O(n2)O(n^2)

O(n)O(n) in the worst case

No

Standard array heapsort

O(nlog⁡n)O(n\log n)

O(1)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

4n24n^2

64 bytes

Q

Yes

12nlog⁡2n12n\log_2 n

16n16n bytes

R

No

8nlog⁡2n8n\log_2 n

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=2112048=2^{11}, its base-two logarithm is ________. The square of the input size is ________.


Substitute these values into the documented bounds:


P(2048)=4(2048)2=4(4194304)=16777216 operations,Q(2048)=12(2048)log⁡22048=12(2048)(11)=270336 operations,R(2048)=8(2048)log⁡22048=8(2048)(11)=180224 operations.\begin{aligned} P(2048)&=4(2048)^2\\ &=4(4194304)\\ &=16777216\ \text{operations},\\ Q(2048)&=12(2048)\log_2 2048\\ &=12(2048)(11)\\ &=270336\ \text{operations},\\ R(2048)&=8(2048)\log_2 2048\\ &=8(2048)(11)\\ &=180224\ \text{operations}. \end{aligned}


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


40 KiB×1024 bytesKiB=40960 bytes.40\ \text{KiB}\times1024\ \frac{\text{bytes}}{\text{KiB}} =40960\ \text{bytes}.


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 ________:


16 bytesrecord×2048 records=32768 bytes.16\ \frac{\text{bytes}}{\text{record}}\times2048\ \text{records} =32768\ \text{bytes}.


Converting Q's requirement gives


32768 bytes1024 bytes/KiB=32 KiB.\frac{32768\ \text{bytes}}{1024\ \text{bytes/KiB}} =32\ \text{KiB}.


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 program sorts an array of nn student scores by rearranging that array. It uses exactly 20 temporary score slots and no function-call stack, regardless of nn. Using the strict constant-auxiliary-space definition, which description is correct?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A library sorts borrowing records by their due dates. Match each sorting term to its meaning when describing the task and checking the output.

Match each term
A.

Rule used to determine the relative order of two values

B.

Arrangement in which each value is less than or equal to the next value

C.

Rearrangement preserving every complete input record and its number of occurrences

D.

Field whose values determine the order of records

Comparator

Permutation

Nondecreasing order

Sort key

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A benchmark planning model assigns implementation A exactly n2n^2 operations and implementation B exactly nlog⁡2nn\log_2 n operations for sorting nn records. Complete the table with exact integer operation counts, using the models rather than measured execution times.

Number of recordsA operationsB operations
128
256

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice