1. Engineering
  2. Computer Science
  3. 6 consider the following algorithm where p is an array...

Question: 6 consider the following algorithm where p is an array...

Question details

6. Consider the following algorithm, where P is an array containing random numbers. The function swap(v1,v2) will swap the values stored in the variables v1 and v2. Note that % is the modulus operation, and will return the integer remainder r of a/b, ie, r-a%b Require: Array P with n >0 values 1: 2: İ=1, jan-1 while i<=j do 4: for a-i to j by 1 do if Pla>Pla+1] and Pla)%2-0 then 6 swap(Plal, Pla+1]) end if 9 end for 10: j-y-1 11 for a-j-1 to i by 1 do 12: if Pla] >Pla-11 and Pla)%2-0 then 13: 14: 15: 16 end for 17: -x+1 18: end while swap(Pla], Pla+i]) end if (a) (1 point) What are the input(s) to the algorithm? (b) (2 points) In one short sentence explain what the purpose of the algorithm is. Be as specific as possible by referring to program variables (c) (10 points) Provide a proof of correctness using a loop invariant. Be sure to clearly state the invariant. HINT: after each iteration of the outer loop, largest even element of the array is always placed at rightmost position. (d) (3 points) What is a best-case runtime scenario for this algorithm? Explain (e) (3 points) What is a worst-case runtime scenario for this algorithm? Explain.

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution