Inversion Count for an array indicates – how far (or close) the array is from being sorted. If array is already sorted then inversion count is 0. If array is sorted in reverse order that inversion count is maximum. Formally speaking, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j
Example: The sequence 2, 4, 1, 3, 5 has three inversions (2, 1), (4, 1), (4, 3).
What would the complexity of a brute force approach be?
Code an O(nlog(n)) algorithm to count the number of inversions. Hint - piggy back on the merging step of the mergesort algorithm.
Complete the code in the functions indicated to count inversions.
countInv.cpp ====== code: (EDIT ONLY THIS FILE)
/ Count inversions - homework
// Based off of mergesort
#include
#include // For copy
using namespace std;
int mergeInv(vector& nums, vector& left, vector& right) {
// You will need this helper function that calculates the inversion while merging
// Your code here
}
int countInv(vector&nums) {
// Your code here
} // END OF COUNTINV.CPP FILE
countInv_test.cpp ===== code: (DO NOT EDIT THIS FILE)
/* Count the number of inversions in O(n log n) time */
#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
} / END OF COUNTINV_TEST.CPP FILE
Code an O(nlog(n)) algorithm to count the number of inversions. Hint - piggy back on the merging step of the mergesort algorithm.
Keep the COUNTINV_TEST.CPP file seperate from the COUNTINV.CPP file when coding.
Thank you so much for your help.
In: Computer Science
The gas phase decomposition of nitrosyl chloride at 400 K NOCl(g)NO(g) + ½ Cl2(g) is second order in NOCl with a rate constant of 5.90×10-4 M-1 s-1. If the initial concentration of NOCl is 3.75×10-2 M, the concentration of NOCl will be 1.05×10-2 M after seconds have passed.
In: Chemistry
in parts a and b use gaussian elimination to solve the system of linear equations. show all algebraic steps.
a. x1 + x2 + x3 = 2
x1 - x3 = -2
2x2 + x3 = -1
b. x1 + x2 + x3 = 3
3x1 + 4x2 + 2x3 = 4
4x1 + 5x2 + 3x3 = 7
2x1 + 3x2 + x3 = 1
In: Advanced Math
A company is evaluating two possible projects with the following cash flows. Suppose that the projects are contingent. Which answer describes the correct decision if the firm's required rate of return is 12%?
|
Year |
|||||
|
0 |
1 |
2 |
3 |
4 |
|
|
Project 1 |
-$50 |
$15 |
$15 |
$5 |
$5 |
|
Project 2 |
-$30 |
$20 |
$10 |
$10 |
$10 |
In: Finance
Research the associated lab and diagnostic studies that are
associated with:
1.Dysphagia
2.Gastroesophageal reflux disease (GERD)
3.Gastroparesis
4.Bowel incontinence.
5.Chronic constipation.
6.Chronic diarrhea.
7.Irritable Bowel Syndrome
Explain for each diagnosis :
1. Diagnostic test
2. Lab test
3. 3 nursing interventions
In: Nursing
Differentiate the following functions, taking care to use the correct notation for the derivative function:
Can I get the step by step solution for this please thanks
1) f(x)=(x2+2x)(3x-9)
2) y=9x2-3x/x-1
3) y = 3(8x2 + 4x)3
4) y=3xex^2
In: Math
Find the derivatives of each of the following functions:
1. f(x) = (3x^2 + 2x − 7)^5 (2x + 1)^8
2. g(t) = cos(e^2x2+8x−3)
3. h(x) = e^x2/tan(2x−3)
4. Find dy/dx if cos(xy) = x^2y^5
In: Math
1) Determine the distance between the point (-2,
6) and the line 3x -4y -10 = 0
2) Determine the distance between the point (4,
-1) and the line 3x -4y +12 = 0
3) Determine the distance between two parallel lines which
equations are 3x -4y +6 = 0 and
6x -8y +9 = 0
In: Math
A sample of 5 months of sales data provided the following information: Month 1 2 3 4 5 Units Sold 96 110 85 96 93 Develop a point estimate of the population mean number of units sold per month (to 1 decimal). Develop a point estimate of the population standard deviation (to 2 decimals).
In: Statistics and Probability
For the following Grouped Frequency Distribution below
:
class 7-12 13-18 19-24 25-30 31-36 37-42 43-48
f 5 6 2 4 1 3 7
1) Find ( Mean , Mode , Standard Deviation , Variance )
2) Sketch : Histogram , Polygon , and an Ogive
In: Statistics and Probability