Question: Given an array $ [ a _ 1 , a _ 2 , dots , a _ n ] $ , a reversal is

Given an array $[a_1, a_2,\dots, a_n]$, a reversal is a pair $(i,j)$ such that $i < j$ but $a_i > a_j$. For example, in the array $[5,3,2,10]$ there are are three reversals ($(1,2),(1,3),(2,3)$). Note that the array has no reversals if and only if it is sorted, so the number of reversals can be thought of as a measure of how well-sorted an array is.\begin{enumerate}\item (11 points) What is the expected number of reversals in a random array? More formally, consider a random permutation of $n$ distinct elements $a_1,\dots, a_n$: what is the expected number of reversals? Give your answer precisely, without asymptotic notation. Prove your answer. \item (11 points) Recall the insertion sort algorithm: \begin{verbatim} for i =1 to n j = i while j >0 and A[j-1]> A[j] swap A[j] and A[j-1] j = j -1\end{verbatim} Suppose that our array has $d$ reversals. Prove that the (worst-case) running time of insertion sort is $\Theta(n + d)$.\item (11 points) What does this imply about the \emph{average-case} running time of insertion sort as a function only of $n$? That is, if we draw a permutation uniformly at random, what is the expected running time of insertion sort (in asymptotic notation)? Note that this is \emph{not} a randomized algorithm; this is a deterministic algorithm on a random input.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!