Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Exercise 2 ( 7 0 points ) Loop Invariant Consider the algorithm getIndexMaximum ( A , k ) that takes a sequence A as an
Exercise points Loop Invariant
Consider the algorithm getIndexMaximumAk that takes a sequence A as an input and returns the
index of the largest number maximum in the range kAlength in Sequence A
For example, let A getIndexMaximumA will return because is the index of
the element and is the largest number in A in the range
Consider the following sorting algorithm that sorts a sequence A in decreasing order:
SortArrayA
for i to Alength
IndexMax getIndexMaximumAi
swap Ai and AIndexMin
buffer AIndexMax
AIndexMax Ai
Ai buffer
The objective is to prove that the above sorting algorithm is correct. Use the textbook in Section :
the authors show that the InsertSort algorithm is correct using loop invariants. Their proof should help you
with this exercise. It is expected and strongly advised that you follow their steps in Section
Advice: get familiar with SortArray by executing the algorithm SortArrayA when A
points Express the property that SortArrayA must satisfy to be correct:
points List four loop invariants even if they are not that helpful for our ultimate proof of
correctness of SortArray Specify whether your loop invariant applies before or after the iteration.
points Propose a loop invariant for the outer loop that is the closest to our ultimate objective:
SortArray is correct.
Use the three steps:
a points Initialization
b points Maintenance
c pointsTermination
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started