OFFSET
0,3
COMMENTS
From Petros Hadjicostas, May 04 2020: (Start)
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!.)
LINKS
Daniel Krenn, Table of n, a(n) for n = 0..100
Pamela E. Harris, Jan Kretschmann, and J. Carlos MartÃnez Mori, Lucky Cars and the Quicksort Algorithm, arXiv:2306.13065 [math.CO], 2023.
FORMULA
a(n) = n!*(2*(n+1)*H(n) - 4*n).
c(n) = a(n) / n! satisfies c(n) = (n-1) + 2/n * Sum_{i < n} c(i).
a(n) = 2*A002538(n-1), n >= 2. - Omar E. Pol, Jun 20 2017
E.g.f.: -2*log(1-x)/(1-x)^2 - 2*x/(1-x)^2. - Daniel Krenn, Jun 20 2017
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
From Petros Hadjicostas, May 03 2020: (Start)
a(n) = A063090(n) - n!*n for n >= 1.
MAPLE
a:= proc(n) option remember; `if`(n<3, n*(n-1),
((2*n^2-3*n-1)*a(n-1)-(n-1)^2*n*a(n-2))/(n-2))
end:
seq(a(n), n=0..25); # Alois P. Heinz, Jun 21 2017
MATHEMATICA
a[n_] := n! (2(n+1)HarmonicNumber[n] - 4n);
a /@ Range[0, 25] (* Jean-François Alcover, Oct 29 2020 *)
PROG
(SageMath) [n.factorial() * (2*(n+1)*sum(1/k for k in srange(1, n+1)) - 4*n) for n in srange(21)]
(SageMath) # alternative using the recurrence relation
@cached_function
def c(n):
if n <= 1:
return 0
return (n - 1) + 2/n*sum(c(i) for i in srange(n))
[n.factorial() * c(n) for n in srange(21)]
CROSSREFS
KEYWORD
nonn
AUTHOR
Daniel Krenn, Jun 20 2017
STATUS
approved