Sorting number

From HandWiki

In mathematics and computer science, the sorting numbers are a sequence of numbers introduced in 1950 by Hugo Steinhaus for the analysis of comparison sort algorithms. These numbers give the worst-case number of comparisons used by both binary insertion sort and merge sort. However, there are other algorithms that use fewer comparisons.

Formula and examples

The [math]\displaystyle{ n }[/math]th sorting number is given by the formula[1]

[math]\displaystyle{ n\,S(n) - 2^{S(n)} + 1, }[/math]


[math]\displaystyle{ S(n) = \lfloor 1 + \log_2 n \rfloor. }[/math]

The sequence of numbers given by this formula (starting with [math]\displaystyle{ n = 1 }[/math]) is

0, 1, 3, 5, 8, 11, 14, 17, 21, 25, 29, 33, 37, 41, ... (sequence A001855 in the OEIS).

The same sequence of numbers can also be obtained from the recurrence relation[2],

[math]\displaystyle{ A(n) = A\bigl(\lfloor n/2\rfloor\bigr) + A\bigl(\lceil n/2\rceil\bigr) + n - 1 }[/math]

or closed form

[math]\displaystyle{ A(n) = n\lceil\log_2 n\rceil - 2^{\lceil\log_2 n\rceil} + 1. }[/math]

It is an example of a 2-regular sequence.[2]

Asymptotically, the value of the [math]\displaystyle{ n }[/math]th sorting number fluctuates between approximately [math]\displaystyle{ n\log_2 n - n }[/math] and [math]\displaystyle{ n\log_2 n - 0.915n, }[/math] depending on the ratio between [math]\displaystyle{ n }[/math] and the nearest power of two.[1]

Application to sorting

In 1950, Hugo Steinhaus observed that these numbers count the number of comparisons used by binary insertion sort, and conjectured (incorrectly) that they give the minimum number of comparisons needed to sort [math]\displaystyle{ n }[/math] items using any comparison sort. The conjecture was disproved in 1959 by L. R. Ford Jr. and Selmer M. Johnson, who found a different sorting algorithm, the Ford–Johnson merge-insert sort, using fewer comparisons.[1]

The same sequence of sorting numbers also gives the worst-case number of comparisons used by merge sort to sort [math]\displaystyle{ n }[/math] items.[2]

Other applications

The sorting numbers (shifted by one position) also give the sizes of the shortest possible superpatterns for the layered permutations.[3]


  1. 1.0 1.1 1.2 "A tournament problem", American Mathematical Monthly 66 (5): 387–389, 1959, doi:10.2307/2308750 
  2. 2.0 2.1 2.2 Allouche, Jean-Paul (1992), "The ring of [math]\displaystyle{ k }[/math]-regular sequences", Theoretical Computer Science 98 (2): 163–197, doi:10.1016/0304-3975(92)90001-V . See Example 28, p. 192.
  3. "Universal layered permutations", Electronic Journal of Combinatorics 25 (3): P23:1–P23:5, 2018, doi:10.37236/7386