UGC-NET Jul 2016 · Asymptotic Analysis & Recurrences

MCQ · 2 marks · Problem PROG-DIVC-0024

Let $A[1,...,n]$ be an array of n distinct numbers. If $i<j$ and $A[i]>A[j]$, then the pair $(i,j)$ is called an inversion of $A$. What is the expected number of inversions in any permutation on $n$ elements?
  1. A
    $\theta(n)$
  2. B
    $\theta(lgn)$
  3. C
    $\theta(nlgn)$
  4. D
    $\theta(n^2)$

Solve it on GATE DA practice →More Searching & Sorting Algorithms problems

Show the worked solution\[\boxed{E[\text{inversions}]=\sum_{i<j}P(A[i]>A[j])=\binom n2\cdot\frac12}\]In a uniformly random permutation each of the \(\binom n2\) pairs is inverted with probability \(1/2\).\[E=\frac{n(n-1)}{4}=\Theta(n^2).\]This is also why insertion sort takes \(\Theta(n^2)\) time on average.Answer: D

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com