edited by
147 views
0 votes
0 votes

Consider the following code, where $A$ is an array of integers of size $\text{size(A)}$ with values $A[0] \;to\;A[\text{size(A)-1}]$, and $\text{reverse(A,i,j)}$ reverses the segment $\text{A[i]}$ to $\text{A[j]}$ if $\text{i<=j}$ and has no effect otherwise. For instance, if $A=[0,1,2,3,4,5]$, then $\text{reverse(A,2,4)}$ would modify $A$ to $[0,1,4,3,2,5].$

def mystery(A)
{
    for j in [0,1,...,size(A)-1]
    {
        p=j;
        for i in [j,j+1,...,size(A)-1]
        {
            if A[i]>A[p]
            {
                p=i;
            }
            reverse(A,j,p);
        }
    }
}
  1. What is the effect of this code on an input array $A$?
  2. Suppose $\text{size(A)}$ is $1000$. How many times is the test $\text{A[i]>A[p]}$ executed?
edited by

1 Answer

Related questions

0 votes
0 votes
1 answer
1
soujanyareddy13 asked Jan 29, 2021
137 views
The identity$$\frac{1}{(1-2r)}=\displaystyle\sum^{\infty} _{k=0} (2r)^k $$is trueif and only if $r\neq \frac{1}{2}$if and only if $0\leq r < \frac{1}{2}$if and only if $-...
1 votes
1 votes
1 answer
2
soujanyareddy13 asked Jan 29, 2021
252 views
A permutation $\sigma$ is a bijection from the set $[n]=\{1,2,\dots ,n\}$ to itself. We denote it using the notation$$\begin{pmatrix}1 & 2 & … & n \\ {\sigma(1)} & {\si...