Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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, i.e., r-a%b Require: Array P with n > 0 values 1: i-1, j-n-l 2: while iPat 11 and Pla]%2--0 then swap(Plal, Pla+1l) end if 9 end for 10: -y-1 1 for a-j-1 toi by1 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+1) x-a 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2018 Dublin Ireland September 10 14 2018 Proceedings Part 1 Lnai 11051

Authors: Michele Berlingerio ,Francesco Bonchi ,Thomas Gartner ,Neil Hurley ,Georgiana Ifrim

1st Edition

3030109240, 978-3030109240

More Books

Students also viewed these Databases questions

Question

Use a three-step process to develop effective business messages.

Answered: 1 week ago