Question

In: Computer Science

Description Inversion Count for an array indicates – how far (or close) the array is from...

Description

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 the 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).

Requirements:

• Design should be based on divide and conquer.

• Running time should NOT be worse than Θ (n log n).

• Must use recursion to subproblems

Input Specification

Your program should read from an input file, which starts with a line indicating the number of test cases. Each of the following lines indicates a test case, and each test case includes a sequence of numbers (separated by spaces) for which you need to count inversions.

Sample Input

3

2 4 1 3 5

1 2 4 8 9 3 5 6

1 20 6 4 5

Output Specification

For each test case, print one line with a format of The sequence has ? inversions in console.

Sample Output

Below is the correct output for the previous sample input.

The sequence has 3 inversions.

The sequence has 7 inversions.

The sequence has 5 inversions.

Hints:

The design can be based on merge sort.

How to get number of inversions in merge()?

Input file

input.txt

3

2 4 1 3 5

1. 2 4 8 9 3 5 6

1. 20 6 4 5

Please code in C++ thank you for your help and time!!

Solutions

Expert Solution

///////////////////////////////////////////////////////////////////////////////////////////////////////////

CODE:-

//////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

int inver = 0;
int curLin = 0;
int noEl = 0;
int element[100]; // Temporary Holding Array

void inverCal(int ele[]) {
   for (int i = 0; i < noEl; i++) {
       for (int j = i + 1; j < noEl; j++) {
           if (ele[i] > ele[j]) {
               inver++;
           }
       }
   }
}

int main() {

int elements[noEl]; // Array that holds elements in current line.   


   std::ifstream file("test.txt");
   std::string str;

   // Below code reads text file line by line.

   while (std::getline(file, str)) {
       std::istringstream is(str);

       int ele;
       int n = 0;
       while (is >> ele) {
           element[n] = ele;
           if (curLin > 0) {

             // This holds no of elements in the current line.Which will be used to give size to our array "elements".
               noEl++;
           }
           n++;
       }
       n = 0;

       // Code below assigns values from temporary array "element" to the array we will use to find inversions "elements".
       if (curLin > 0) {
           for (int i = 0; i < noEl; i++) {
               elements[i] = element[i];
           }

           // Method below takes integer array as an argument and finds the inversions.
           inverCal(elements);
           std::cout << "The sequence has " << inver << " inversions." << "\n";
           inver = 0;
           noEl = 0;
       }

      // Code below increment the value of current line to 1 to indicate actual values are starting now.
       if (curLin == 0) {
           curLin++;
       }
   }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

Input :-

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

3
2 4 1 3 5
1 2 4 8 9 3 5 6
1 20 6 4 5

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

Output :-

//////////////////////////////////////////////////////////////////////////////////////////////////////////////


Related Solutions

Inversion Count for an array indicates – how far (or close) the array is from being...
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...
Let "fit" summarize how far the in-sample observations are from the fitted model; e.g., fit=100 indicates...
Let "fit" summarize how far the in-sample observations are from the fitted model; e.g., fit=100 indicates worse fit than fit=50. Let "penalty" be increasing in the size of a model, specifically the number of parameters in the model, given the same sample size; e.g., given sample size 80, penalty is larger for a model with 10 parameters than for a model with 5 parameters. To prevent overfitting when choosing a forecasting model, you could maximize (fit + penalty) minimize (fit...
Is New Zealand far or close from a steady state economy and what is their depreciation...
Is New Zealand far or close from a steady state economy and what is their depreciation rate?
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Based on the source code below, add codes to count how many times array has been...
Based on the source code below, add codes to count how many times array has been access by this program in sorting the array using the insertion sort. Explain how did you count the number of array access on this code. Note that this program tests on 100 until 5000 integers, with increment of 100. import random #create randomized array of length 'length' from range 0 up to max def create_array(length=100, max=5000): return [random.randint(0,max) for _ in range(length)] #executes insertion...
Swayamvar Problem Description A ceremony where a Bride chooses her Groom from an array of eligible...
Swayamvar Problem Description A ceremony where a Bride chooses her Groom from an array of eligible bachelors is called Swayamvar. But this is a Swayamvar with difference. An array of Bride-to-be will choose from an array of Groom-to-be. The arrangement at this Swayamvar is as follows · Brides-to-be are organized such that the most eligible bachelorette will get first chance to choose her Groom. Only then, the next most eligible bachelorette will get a chance to choose her Groom ·...
how are the distributions of gains from trade changing as far as labor and capital are...
how are the distributions of gains from trade changing as far as labor and capital are concerned? can we still use the h-0 model? The topic is about globalization inequality. Please type the answer. Thanks
write code to count the number of odd integers in an array of 100 random integers...
write code to count the number of odd integers in an array of 100 random integers in the range [0,99].
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT