edited by
1,342 views
6 votes
6 votes

The below question is based on following program:

procedure mystery (A : array [1..100] of int)
    int i,j,position,tmp;
    begin
        for j := 1 to 100 do
            position := j;
            for i := j to 100 do
                if (A[i] > A[position]) then
                    position := i;
                endfor
            tmp := A[j];
            A[j] := A[position];
            A[position] := tmp;
        endfor
end

The number of times the test $A[i] > A[\text{position}]$ is executed is:

  1. $100$
  2. $5050$
  3. $10000$
  4. Depends on contents of $A$
edited by

3 Answers

4 votes
4 votes

for i=1.. Inner Loop will run 100 times

for i=2.. Inner Loop will run 99 times

...

...

for i=100.. Inner Loop will run 1 time.

so total no of time given statement executed is 1+2+...+100= (100*101)/2=5050

Answer:

Related questions

32 votes
32 votes
5 answers
2
go_editor asked May 23, 2016
6,641 views
You have $n$ lists, each consisting of $m$ integers sorted in ascending order. Merging these lists into a single sorted list will take time:$O(nm \log m)$$O(mn \log n)$...