Question: Merge ( A , p , q , r ) n L = q - p + 1 , / / length of A [

Merge(A,p,q,r)
nL=q-p+1,// length of A[p:q]
nR=r-q,// length of A[q+1:r]
let L[0:nL-1] and R[0:nR-1] be new arrays
for i=0 to nL-1,?? copy A[p:q] into L[0:nL-1]
L[i]=A[p+i]
for j=0 to nR-1?? copy A[q+1:r] into R[0:nR-1]
R[j]=A[q+j+1]
i=0,??i indexes the smallest remaining element in L
j=0,??j indexes the smallest remaining element in R
k=p,??k indexes the location in A to fill
// As long as each of the arrays L and R contains an unmerged element,
// copy the smallest unmerged element back into A[p:r].
while L[i]R[j]A[k]=L[i]i=i+1A[k]=R[j]j=j+1k=k+1LRA[p:r]A[k]=L[i]i=i+1k=k+1A[k]=R[j]j=j+1k=k+1j
A[k]=R[j]
j=j+1
k=k+1i
A[k]=L[i]
i=i+1
k=k+1
while j
A[k]=R[j]
j=j+1
k=k+1j
ifL[i]R[j]
A[k]=L[i]
i=i+1
else A[k]=R[j]
j=j+1
k=k+1???
Having gone through one ofL and R entirely, copy the???
remainder of the other to the end ofA[p:r].
while i
A[k]=L[i]
i=i+1
k=k+1
while j
A[k]=R[j]
j=j+1
k=k+1i and j
ifL[i]R[j]
A[k]=L[i]
i=i+1
else A[k]=R[j]
j=j+1
k=k+1???
Having gone through one ofL and R entirely, copy the???
remainder of the other to the end ofA[p:r].
while i
A[k]=L[i]
i=i+1
k=k+1
while j
A[k]=R[j]
j=j+1
k=k+1
State a loop invariant for the while loop of lines 12318 of the MERGE procedure.
Show how to use it, along with the while loops of lines 20323 and 24327, to prove
that the MERGE procedure is correct.
 Merge(A,p,q,r) nL=q-p+1,// length of A[p:q] nR=r-q,// length of A[q+1:r] let L[0:nL-1]

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 Databases Questions!