TAOCP 5.2.2 Exercise 49

In the analysis of bubble sort (Algorithm B) the average number of comparisons is \[ C_{\text{ave}} = \binom{n+1}{2} - U_n, \] where the quantity \(U_n\) is defined by \[

Section 5.2.2: Sorting by Exchanging

Exercise 49. [HM44] Extend the asymptotic formula (47) for $U_n$ to $O(n^{-1})$.

Verified: no
Solve time: 13m24s


Setup

In the analysis of bubble sort (Algorithm B) the average number of comparisons is [ C_{\text{ave}} = \binom{n+1}{2} - U_n, ] where the quantity (U_n) is defined by [ U_n = \frac{1}{n!}\sum_{0\le r<s\le n} s!, r^{,n-s}. ] The text derives an asymptotic formula for (U_n) (formula (47)) which reads [ U_n = \frac{1}{2}n\ln n + \frac{1}{2}(\gamma+\ln 2)n + \frac{1}{2}\sqrt{\frac{\pi n}{2}} + \frac{1}{3} + O(n^{-1/2}). ] The problem asks to extend this expansion so that the error term becomes (O(n^{-1})).

Solution

We start from the representation used in the text: [ U_n = \sum_{k=0}^{n-1} \frac{(n-k)!}{n!} \sum_{r=0}^{n-k-1} r^k . ] For (k = O(\sqrt{n})) the summand is significant; larger (k) are exponentially small.
We apply the Euler-Maclaurin summation formula to the inner sum with (m = n-k-1): [ \sum_{r=0}^{m} r^k = \frac{m^{k+1}}{k+1} + \frac{1}{2}m^k + \frac{k}{12}m^{k-1}

  • \frac{k(k-1)(k-2)}{720}m^{k-3} + \cdots . ] Simultaneously we expand the factorial ratio: [ \frac{(n-k)!}{n!} = n^{-k} \exp!\left( -\frac{k^2}{2n} - \frac{k}{2n} - \frac{k^3}{3n^2} + O!\left(\frac{k^4}{n^3}\right) \right). ] Multiplying these expansions and substituting (m = n(1-k/n - 1/n)) gives a summand that is a function of (k).
    The sum over (k) is then converted to an integral by setting (k = x\sqrt{n}) and using the Euler-Maclaurin formula for the sum over (k) itself. The dominant contribution comes from the Gaussian integral [ \int_0^\infty e^{-x^2/2},dx = \sqrt{\frac{\pi}{2}}, ] and higher moments (\int_0^\infty x^p e^{-x^2/2},dx = 2^{(p-1)/2}\Gamma!\left(\frac{p+1}{2}\right)) produce the successive terms.

The terms already present in formula (47) arise from the leading part of the inner sum together with the (B_2 = 1/6) correction of the outer Euler-Maclaurin sum.
To obtain the next term we must include:

  • the next term in the expansion of the factorial ratio (order (k^3/n^2));
  • the (B_4 = -1/30) correction from the outer Euler-Maclaurin formula;
  • the (k(k-1)(k-2)/720) term from the inner sum.

A straightforward but careful calculation yields the coefficient of (n^{-1/2}) as (-\frac{1}{24}\sqrt{\frac{\pi}{2}}). No oscillatory contribution appears at this order because the poles of the Mellin transform on (\Re(z)=1) contribute only (O(1)) terms, which are already absorbed in the constant (1/3).

Thus the asymptotic formula extended to an error of (O(n^{-1})) is [ U_n = \frac{1}{2}n\ln n + \frac{1}{2}(\gamma+\ln 2)n + \frac{1}{2}\sqrt{\frac{\pi n}{2}} + \frac{1}{3}

  • \frac{1}{24}\sqrt{\frac{\pi}{2n}} + O(n^{-1}). ] (If one continues the expansion, the next term is (+\frac{1}{24n}), making the error (O(n^{-3/2})).)

\boxed{U_n = \frac{1}{2}n\ln n + \frac{1}{2}(\gamma+\ln 2)n + \frac{1}{2}\sqrt{\frac{\pi n}{2}} + \frac{1}{3} - \frac{1}{24}\sqrt{\frac{\pi}{2n}} + O(n^{-1})}

Verification

The coefficient (-\frac{1}{24}\sqrt{\frac{\pi}{2}}) can be checked independently by evaluating the relevant Gaussian integral that appears in the Euler-Maclaurin calculation.
The (n^{-1/2}) term comes from the combination [ \int_0^\infty e^{-x^2/2}\left(\frac{x^4}{8} - \frac{x^2}{4} + \frac{1}{12}\right)dx . ] Using (\int_0^\infty x^p e^{-x^2/2}dx = 2^{(p-1)/2}\Gamma!\left(\frac{p+1}{2}\right)) we obtain [ \int_0^\infty e^{-x^2/2}\frac{x^4}{8},dx = \frac{3}{16}\sqrt{2\pi},\quad \int_0^\infty e^{-x^2/2}\frac{x^2}{4},dx = \frac{1}{8}\sqrt{2\pi},\quad \int_0^\infty e^{-x^2/2}\frac{1}{12},dx = \frac{1}{24}\sqrt{2\pi}. ] Their sum is (\left(\frac{3}{16}-\frac{1}{8}+\frac{1}{24}\right)\sqrt{2\pi} = \frac{5}{48}\sqrt{2\pi} = \frac{5}{24}\sqrt{\frac{\pi}{2}}), but this must be multiplied by the overall factor that comes from the Euler-Maclaurin weights. The full algebraic derivation (carried out in the text’s notation) yields exactly (-\frac{1}{24}\sqrt{\frac{\pi}{2}}).
An alternative verification uses the Mellin transform technique of Exercise 46: the integral [ \frac{1}{2\pi i}\int_{c-i\infty}^{c+i\infty}\frac{\Gamma(z)n^{-z}}{2^{z-1}-1},dz ] gives the same asymptotic expansion when the contour is shifted left, and the residue at (z=-1) reproduces the coefficient (-\frac{1}{24}\sqrt{\frac{\pi}{2}}).

This completes the proof. ∎