Question

In: Computer Science

c++ is the language Code an O(nlog(n)) algorithm to count the number of inversions DO NOT...

c++ is the language

Code an O(nlog(n)) algorithm to count the number of inversions

DO NOT ADD OR EDIT FUNCTIONS USE THESE ONLY

DO NOT EDIT THE TEST FILE EITHER

countInv.cpp

#include <vector>
#include <algorithm>

using namespace std;


int mergeInv(vector<int>& nums, vector<int>& left, vector<int>& right) {
// You will need this helper function that calculates the inversion while merging
// Your code here


}

int countInv(vector<int>&nums) {
// Your code here


}

countInv_test.cpp

#include <iostream>
#include <vector>


using namespace std;


int countInv(vector<int>& numvec);

int main()
{
    int n;
    vector<int> numvec{4, 5, 6, 1, 2, 3};
    n = countInv(numvec);
    cout << "Number of inversions " << n << endl; // Should be 9
  
    numvec = {1, 2, 3, 4, 5, 6};
    n = countInv(numvec);
    cout << "Number of inversions " << n << endl; // Should be 0
  
    numvec = {6, 5, 4, 3, 2, 1};
    n = countInv(numvec);
    cout << "Number of inversions " << n << endl; // Should be 15
  
    numvec = {0, 0, 0, 0, 0, 0};
    n = countInv(numvec);
    cout << "Number of inversions " << n << endl;; // Should be 0
}

Solutions

Expert Solution

Count Inversion can be solved using merge sort technique in O(nlogn) complexity.

Step1 : Divide the array into two parts recursively then call the merge function to arrange the array in sorted manner.

Step2 : While Merging , whenever element in left part is greater than the element in right part , it means that all the rest of the elements in the left part including the current element satisfies this condition . So we add all
remaining elements of the left part in our answer.

Code Explanation:

1 - Divide the array into two parts i.e left and right
Note: if array length is 1 then we will simply return 0 as inversion is possible only in pairs.

2 - we call the function recursively on left and right and then merge them.

3 - Merge left and right and whenever left[i] > right[j] all the afterwards elements of left i.e [ left.size()-i] gets added to the answer.

Code :

#include <iostream>
#include <vector>

using namespace std;

int countInv(vector<int>& numvec);
int mergeInv(vector<int>& nums, vector<int>& left, vector<int>& right) {
int i=0,j=0,k=0,ans=0;
while(i<left.size() && j<right.size())
{
if(left[i] <= right[j]){
nums[k] = left[i];
i+=1;
}
else{
ans += (left.size()-i);//this is the number of inversions
nums[k] = right[j];
j+=1;
}
k+=1;
}
while(i<left.size()){
nums[k] = left[i];
i+=1;
k+=1;
}
while(j<right.size()){
nums[k] = right[j];
j+=1;
k+=1;
}
return ans;
}

int countInv(vector<int>&nums) {
int n = nums.size();
if(n==1)
return 0;
vector<int> left,right;
//Dividing the vector in two parts i.e left and right
for(int i=0;i<(n/2);i++)
left.push_back(nums[i]);
  
for(int i=(n/2);i<n;i++)
right.push_back(nums[i]);
  
return countInv(left) + countInv(right) + mergeInv(nums,left,right);
}
int main()
{
int n;
vector<int> numvec{4, 5, 6, 1, 2, 3};
n = countInv(numvec);
cout << "Number of inversions " << n << endl; // Should be 9
numvec = {1, 2, 3, 4, 5, 6};
n = countInv(numvec);
cout << "Number of inversions " << n << endl; // Should be 0
numvec = {6, 5, 4, 3, 2, 1};
n = countInv(numvec);
cout << "Number of inversions " << n << endl; // Should be 15
numvec = {0, 0, 0, 0, 0, 0};
n = countInv(numvec);
cout << "Number of inversions " << n << endl;; // Should be 0
}


Related Solutions

Fix the C++ code to count the number of inversions using the given array. The answer...
Fix the C++ code to count the number of inversions using the given array. The answer is 8 inversions, but I am only getting 6 inversions. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include <iostream> using namespace std; int mergeInversion(int arr[], int l, int m, int r) {     // size of the two arrays     int n1 = m - l + 1;     int n2 = r - m;     // create temporary arrays     int L[n1];     int R[n2];     // Copy the...
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find...
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find out the total number of inversions in the given sequence. For example, for the sequence of 2, 4, 1, 3, 5, there are three inversions (2,1), (4,1), and (4,3). Give a brute-force algorithm with running time of O(n^2). Using the technique of divide-and-conquer, design an algorithm with running time of O(nlog n).
Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements in the arrangement: element [0]: 5 element [1]: 1 element [2]: 1 Expected output: The total number of duplicate elements found in the array is: 1
write a program in c language to count the number of spaces ina sentence .the sentence...
write a program in c language to count the number of spaces ina sentence .the sentence is saved in the string 'sentence' write it in a repl.it and along with input,output and algorithm
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes the final score of a baseball game. Use a loop to read the number of runs scored by both teams during each of nine innings. Display the final score afterward. Submit your design, code, and execution result via file, if possible
Code in C-language programming description about convert binary number to decimal number.
Code in C-language programming description about convert binary number to decimal number.
Given an array A of n integers, describe an O(n log n) algorithm to decide if...
Given an array A of n integers, describe an O(n log n) algorithm to decide if the elements of A are all distinct. Why does your algorithm run in O(n log n) time?
Write code for O(n) worst-case algorithm that verifies that a tree is actually an AVL tree by detecting imbalance.
Write code for O(n) worst-case algorithm that verifies that a tree is actually an AVL tree by detecting imbalance. If imbalance is true, then call the proper rotation function provided in the lecture slides to fix the imbalance condition.1. Must read AVLNode data from a text file2. Create a Book Object; and an AVL node object to be inserted into the AVL tree3. At each insert, detect imbalance and fix the AVL tree4. Report each imbalance detection and the node...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT