Question: Recall the Insertion - Sort algorithm InSERTION - Sort ( A , n ) for j = 2 to n key = A [ j

Recall the Insertion-Sort algorithm
InSERTION-Sort (A,n)
for j=2 to n
key =A[j]
i=j-1
while i>0 and A[i]>key
A[i+1]=A[i]
,i=i-1
A[i+1]= key
You have seen a correctness proof of the algorithm based on the following loop invariant for the (outer) for loop:
Let Aj[1,dots,n] denote the array at the beginning of iteration j(end of iteration j-1). We have that Aj[1,dots,j-1] stores the same values as A[1,dots,j-1] but in sorted order, while Aj[l]=A[l] for jln.
In this problem, you will fill in a bit more detail in the proof, by also introducing a loop invariant for the (inner) while loop. You will use the following loop invariant:
Let Aj,i[1,dots,n] denote the array at the beginning of iteration i of the inner loop (for 0ij-1). Then:
If the loop executes with value i+1, then
Aj,i[1,dots,i]=Aj[1,dots,i], and
If i+2j, then Aj,i[i+2,dots,j]=Aj[i+1,dots,j-1] and
Recall the Insertion - Sort algorithm InSERTION -

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!