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

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).
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
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.
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...
In a C language program CODE BLOCKS !!!!!!!! Program that stores the number of computers sold...
In a C language program CODE BLOCKS !!!!!!!! Program that stores the number of computers sold by three vendors in four different zones. Is required:  Request the sale amount of each seller by zone, the values must be entered through the keyboard and validate that it does not accept negative numbers.  Menu that requests the operation to be carried out. In case of entering an invalid data, send an error message and request the operation again.  Option...
In a C language program CODE BLOCKS !!!!!!!! Program that stores the number of votes obtained...
In a C language program CODE BLOCKS !!!!!!!! Program that stores the number of votes obtained by 3 amounts in five different zones. Is required: + Request the total of votes by zone of each candidate, the values must be entered on the keyboard and validate that it does not accept negative numbers + Menu that requests the operation to be performed, in case of entering an invalid data send an error message and request the operation again + Option...
Present an O(n) algorithm that sorts n positive integer numbers a1, a2, . . . ,...
Present an O(n) algorithm that sorts n positive integer numbers a1, a2, . . . , an which are known to be bounded by n 2 (so ai ≤ n 2 , for every i = 1, . . . , n. Use the idea of Radix Sort (discussed in class and presented in Section 8.3 in the textbook). Illustrate your algorithm by showing on paper similar to Fig. 8.3, page 198 in the textbook (make sure you indicate clearly...
Determine the number of valence elections in each of the atoms listed below: C, O, N,...
Determine the number of valence elections in each of the atoms listed below: C, O, N, F
what is the maximum number of inversions that a permutation {1,..,n} can have? give the unique...
what is the maximum number of inversions that a permutation {1,..,n} can have? give the unique permutation with this many inversions. how many permutations have one fewer inversions than the minimum.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT