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: $100$ $5050$ $10000$ Depends on contents of $A$ Algorithms cmi2013 algorithms time-complexity + – go_editor 2.1k views answer comment Share Follow Print See all 3 Comments 3 3 Comments reply Manu Thakur commented Dec 23, 2017 reply Follow flag This procedure is similar to selection sort to sort in descending order. maximum element in the unsorted part will be swapped with first element. Array is divided into two parts between sorted and unsorted. After each pass of outer loop, sorted part is increasing and unsorted part decreasing. $T(n) = T(n-1) + n$ answer will be $\frac{n*(n+1)}{2} = 5050$ 10 10 replyShare meghna commented Jul 9, 2018 reply Follow flag @ Manu Thakur how did you wrote this T(n) please explain.?? 0 0 replyShare Sourav Basu commented Nov 23, 2018 reply Follow flag If problem size is n(size of the unsorted half is n) then after complete iteration of inner for loop(which will run for n times) , size of the unsorted part will be reduced by 1(we will have the greatest element of the unsorted part at the left most position hence size of the unsorted part will be n-1 ). Hence , T(n)=T(n−1)+n [ T(n-1) time for reduced problem size and n for the execution of inner for loop n times] 1 1 replyShare Please log in or register to add a comment.
Best answer 11 11 votes Answer: $5050 \ ( 100 +99 + 98 + ..... +1 = (100 *101)/2)$ vijaycs answered May 23, 2016 • edited Jun 24, 2018 by Milicevic3306 vijaycs comment Share Follow 0 reply Please log in or register to add a comment.
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 sonu answered May 23, 2016 sonu comment Share Follow See 1 comment 1 1 comment reply vcrname9295 commented Dec 1, 2018 reply Follow flag what will be worst case time complexity? please explain/., 0 0 replyShare Please log in or register to add a comment.
0 0 votes Answer: soujanyareddy13 answered May 7, 2021 soujanyareddy13 comment Share Follow 0 reply Please log in or register to add a comment.