Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The Number of Inversions. A use case of divide and conquer. Given an integer array A,A[i] and A[j] are inverted if A[i]>A[j]A1(A[2]=4)5-44-2,4-3,5-25-3,5-4,8-2,8-3,8-6,8-7,9-2,9-3,9-6,9-7,9-8r>pq=flinfty r((p+r)/(2))=(A,p,q)=(A,q+1,r)=MERGE(A,p,q,r)MERGE(A,p,q,x)A[p..x]B[pdotsr]=A[pdotsr]i=p;j=q+1;z=p;A[z]=B[i];i++A[z]=B[j];j++;z++A[zdotsr]=B[i.q]A[zdotsr]=B[jdotsr]j A[zdotsr]=B[jdotsr]()/( L)
The Number of Inversions. A use case of divide and conquer.\ Given an integer array
A,A[i]
and
A[j]
are inverted if
A[i]>A[j]A1(A[2]=4)5-44-2,4-3,5-25-3,5-4,8-2,8-3,8-6,8-7,9-2,9-3,9-6,9-7,9-8r>pq=fl\\\\infty r((p+r)/(2))=(A,p,q)=(A,q+1,r)=MERGE(A,p,q,r)MERGE(A,p,q,x)A[p..x]B[pdotsr]=A[pdotsr]i=p;j=q+1;z=p;A[z]=B[i];i++A[z]=B[j];j++;z++A[zdotsr]=B[i.q]A[zdotsr]=B[jdotsr]j\ A[zdotsr]=B[jdotsr]()/( L)\ code herei then\ A[zdotsr]=B[i.q]\ while if j\ A[zdotsr]=B[jdotsr]()/( L)\ code hereB[i] then {\ A[z]=B[i];i++;\ }\ else\ A[z]=B[j];j++;\ (l)/(c)ode here\ j do {\ if B[i] then {\ A[z]=B[i];i++;\ }\ else\ A[z]=B[j];j++;\ (l)/(c)ode here\ }\ z++\ i and j do {\ if B[i] then {\ A[z]=B[i];i++;\ }\ else\ A[z]=B[j];j++;\ (l)/(c)ode here\ }\ z++\ i and A[i]>A[j].\ Consider the following array A with 10 elements. In it, 1(A[2]=4), so value\ pair 5-4 is an inversion.\ In the array above, there are a total of 15 inversions (by the array element values): 4-2,4-3,5-2,\ 5-3,5-4,8-2,8-3,8-6,8-7,9-2,9-3,9-6,9-7,9-8, and 10-7.\ Below is the pseudo-code of the recursive MergeSort algorithm talked in class. How would you\ modify them so that they can help count the number of the inversions?\ MERGE-SORT (A, p, r)\ if r>p then 1\ q=fl\\\\infty r((p+r)/(2))\ inv_(l)eft = MERGE-SORT (A,p,q)\ inv_(r)ight = MERGE-SORT (A,q+1,r)\ inv_(c)ross =MERGE(A,p,q,r)()/(l)\ code here\
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