login

Year-end appeal: Please make a donation to the OEIS Foundation to support ongoing development and maintenance of the OEIS. We are now in our 61st year, we have over 378,000 sequences, and we’ve reached 11,000 citations (which often say “discovered thanks to the OEIS”).

Number of key comparisons to sort all n! permutations of n elements by quicksort.
20

%I #67 Jun 27 2023 12:01:42

%S 0,0,2,16,116,888,7416,67968,682272,7467840,88678080,1136712960,

%T 15655438080,230672171520,3621985113600,60392753971200,

%U 1065907048550400,19855856150323200,389354639411404800,8017578241634304000,172991656889856000000,3903132531903897600000

%N Number of key comparisons to sort all n! permutations of n elements by quicksort.

%C From _Petros Hadjicostas_, May 04 2020: (Start)

%C Depending on the assumptions used in the literature, the average number to sort n items in random order by quicksort appears as -C*n + 2*(1+n)*HarmonicNumber(n), where C = 2, 3, or 4. (To get the number of key comparisons to sort all n! permutations of n elements by quicksort, multiply this number by n!.)

%C For this sequence, we have C = 4. The corresponding number of key comparisons to sort all n! permutations of n elements by quicksort when C = 3 is A063090(n). Thus, a(n) = A063090(n) - n!*n.

%C For more details, see my comments and references for sequences A093418, A096620, and A115107. (End)

%H Daniel Krenn, <a href="/A288964/b288964.txt">Table of n, a(n) for n = 0..100</a>

%H Pamela E. Harris, Jan Kretschmann, and J. Carlos Martínez Mori, <a href="https://arxiv.org/abs/2306.13065">Lucky Cars and the Quicksort Algorithm</a>, arXiv:2306.13065 [math.CO], 2023.

%H <a href="/index/So#sorting">Index entries for sequences related to sorting</a>

%F a(n) = n!*(2*(n+1)*H(n) - 4*n).

%F c(n) = a(n) / n! satisfies c(n) = (n-1) + 2/n * Sum_{i < n} c(i).

%F a(n) = 2*A002538(n-1), n >= 2. - _Omar E. Pol_, Jun 20 2017

%F E.g.f.: -2*log(1-x)/(1-x)^2 - 2*x/(1-x)^2. - _Daniel Krenn_, Jun 20 2017

%F a(n) = ((2*n^2-3*n-1)*a(n-1) -(n-1)^2*n*a(n-2))/(n-2) for n >= 3, a(n) = n*(n-1) for n < 3. - _Alois P. Heinz_, Jun 21 2017

%F From _Petros Hadjicostas_, May 03 2020: (Start)

%F a(n) = A063090(n) - n!*n for n >= 1.

%F a(n) = n!*A115107(n)/A096620(n) = n!*(-n + A093418(n)/A096620(n)). (End)

%p a:= proc(n) option remember; `if`(n<3, n*(n-1),

%p ((2*n^2-3*n-1)*a(n-1)-(n-1)^2*n*a(n-2))/(n-2))

%p end:

%p seq(a(n), n=0..25); # _Alois P. Heinz_, Jun 21 2017

%t a[n_] := n! (2(n+1)HarmonicNumber[n] - 4n);

%t a /@ Range[0, 25] (* _Jean-François Alcover_, Oct 29 2020 *)

%o (SageMath) [n.factorial() * (2*(n+1)*sum(1/k for k in srange(1, n+1)) - 4*n) for n in srange(21)]

%o (SageMath) # alternative using the recurrence relation

%o @cached_function

%o def c(n):

%o if n <= 1:

%o return 0

%o return (n - 1) + 2/n*sum(c(i) for i in srange(n))

%o [n.factorial() * c(n) for n in srange(21)]

%Y Cf. A063090, A093418, A096620, A115107, A117627, A117628, A159324, A288965.

%K nonn

%O 0,3

%A _Daniel Krenn_, Jun 20 2017