edited by
2,072 views
6 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$

3 Answers

4 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:
Position:
Show:

Related questions

9 9 votes
3 answers 3 answers
1.7k
1.7k views
go_editor asked May 23, 2016
1,715 views
The below question is based on the 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 :...
33 33 votes
5 answers 5 answers
10.3k
10.3k views
go_editor asked May 23, 2016
10,263 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)$...
2 2 votes
3 3 answers
2.8k
2.8k views
Arjun asked Jun 8, 2016
2,762 views
Your final exams are over and you are catching up on watching sports on TV. You have a schedule of interesting matches coming up all over the world during the next week. ...
1 1 vote
2 2 answers
1.4k
1.4k views
go_editor asked May 23, 2016
1,418 views
Your final exams are over and you are catching up on watching sports on TV. You have a schedule of interesting matches coming up all over the world during the next week. ...