Answered step by step
Verified Expert Solution
Link Copied!

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\
image text in transcribed
The Number of Inversions. A use case of divide and conquer. Given an integer array A,A[i] and A[i] are inverted if iA[i]. 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, 53,54,82,83,86,87,92,93,96,97,98, and 107. 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=floor((p+r)/2) inv_left =MERGESORT(A,p,q) inv_right =MERGESORT(A,q+1,r) inv_cross =MERGE(A,p,q,r) //code here MERGE (A, p,q,x) // Create a copy of A[p..x] for replacement later B[pr]=A[pr] i=p;j=q+1;z=p; //code here

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

Database Driven Web Sites

Authors: Mike Morrison, Joline Morrison

1st Edition

061901556X, 978-0619015565

More Books

Students also viewed these Databases questions