OFFSET
0,3
COMMENTS
LINKS
Alois P. Heinz, Table of n, a(n) for n = 0..448
Wikipedia, Insertion sort
FORMULA
EXAMPLE
a(0) = a(1) = 0 because 0 or 1 elements are already sorted.
a(2) = 3: [1,2] is sorted and [2,1] needs 3 moves.
a(3) = 23: [1,2,3]->(0), [1,3,2]->(3), [2,1,3]->(3), [2,3,1]->(4), [3,1,2]->(6), [3,2,1]->(7); sum of all moves gives 0+3+3+4+6+7 = 23.
MAPLE
a:= proc(n) option remember;
`if`(n=0, 0, a(n-1)*n + (n-1)! * (n-1)*(n+4)/2)
end:
seq(a(n), n=0..30);
# second Maple program:
a:= proc(n) option remember; `if`(n<3, [0$2, 3][n+1],
((2*n^3+3*n^2-13*n+4)*a(n-1) -(n+4)*
(n-1)^3*a(n-2)) / ((n-2)*(3+n)))
end:
seq(a(n), n=0..30);
MATHEMATICA
a[n_] := n!*(n*(n+7)/4 - 2*HarmonicNumber[n]); Table[a[n], {n, 0, 30}] (* Jean-François Alcover, Feb 01 2017, from 2nd formula *)
CROSSREFS
KEYWORD
nonn
AUTHOR
Alois P. Heinz, May 14 2012
STATUS
approved