Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you convert this to c please? class Code { static int count_inversion_brute(int[] a) { int count=0; //int num_exe=0; for (int i = 0; i

Can you convert this to c please?

class Code

{

static int count_inversion_brute(int[] a)

{

int count=0;

//int num_exe=0;

for (int i = 0; i < a.length - 1; i++){

for (int j = i+1; j < a.length; j++)

if (a[i] > a[j])

count++;

}

return count;

}

static int merge(int[] a, int l, int r, int m, int[] temp)

{

int count=0; // inversion count

int i =l;

int j = m;

int k =l; // for temp index

while((i

if(a[i]<=a[j]){

temp[k++]=a[i++];

}

else{

temp[k++]=a[j++];

count+=(m-i); // bcoz if a[j]>a[i] and both subarrays are sorted so a[i]

//will be less than all a[j+1], a[j+2], ...a[mid]

}

}

//for remaining elements

while(i

temp[k++]=a[i++];

while(j<=r)

temp[k++]=a[j++];

// modify original Array

for(int k1=l; k1

a[k1]=temp[k1];

return count;

}

static int count_inversion_opt(int[] a, int[] temp, int left, int right)

{

//we can achive using merge function

//int[] temp = new int[a.length];

int count=0;

int mid=0;

if(left

mid=(left+right)/2;

count +=count_inversion_opt(a, temp, left, mid);

count+=count_inversion_opt(a, temp, mid+1, right);

count+=merge(a, left, right, mid+1, temp);

}

return count;

}

public static void main (String[] args) throws java.lang.Exception

{

// your code goes here

int[] a = {1, 20, 6, 4, 5};

int[] temp=new int[a.length];

System.out.println("brute force solution "+count_inversion_brute(a));// O(n^2)

System.out.println("optimal solution "+count_inversion_opt(a, temp, 0, a.length-1));

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 Administrator Limited Edition

Authors: Martif Way

1st Edition

B0CGG89N8Z

More Books

Students also viewed these Databases questions

Question

Howacd cash by the end of August

Answered: 1 week ago