edited by
388 views
1 1 vote

Consider the following pseudocode for a function that operates on an $N$ element array $A[1],A[2],\dots,A[N]$ of integers.

function mystery (A[1...N])
{
    int i,j,position,tmp;
    for i=1 to N
    {
        position=i;
        for j=i+1 to N
        {
            if(A[j]<A[position])
            {
                position=j;
            }
        }
        tmp=A[i];
        A[i]=A[position];
        A[position]=tmp;
    }
}
  1. Explain what effect the function has on the input array $A$.
  2. If $N=100$, how many times is the comparison $A[i]<A[position]$ checked?

1 Answer

Position:
Show:

Related questions

0 0 votes
1 1 answer
350
350 views
soujanyareddy13 asked Jan 29, 2021
350 views
The sum of the diagonal elements of a matrix $A$ is called the trace of $A$ and is denoted by $tr(A)$. Which of the following statements about the trace are true?$\text{t...
0 0 votes
1 1 answer
313
313 views
soujanyareddy13 asked Jan 29, 2021
313 views
For numerical answers, the following forms are acceptable: fractions, decimals, symbolic e.g.:$\left( \begin{array}{c} n \\ r \end{array} \right)^n P_r , n!$ etc.For posi...
0 0 votes
1 1 answer
381
381 views
soujanyareddy13 asked Jan 29, 2021
381 views
$\text{Description of the following question:}$Suppose $X$ is the number of successes out of $n$ trials, where the trails are independent of each other. The probability o...
0 0 votes
1 1 answer
369
369 views
soujanyareddy13 asked Jan 29, 2021
369 views
Suppose $X$ is a continuous distribution with probability density function$$f(x)=k(x-x^2),\;0\leq x\leq 1,$$where $k$ is the normalizing constant. Find the value of $k$ a...