Question: i need C++ code for the following algorithm so i can incorporate it into the code included(counting inversions)(it must be O(nlogn) #include #include using namespace
i need C++ code for the following algorithm so i can incorporate it into the code included(counting inversions)(it must be O(nlogn)

#include #include using namespace std; #define SIZE 100001
int main(){
unsigned int a[SIZE]; // permutation/sequence unsigned int n,temp; unsigned int b = 0; //b -- the total number of inversions
//INPUT (from standard input, can use Unix input redirection '> n; //read in the length of the sequence for (int i = 0;i > temp; // read in n numbers of the sequence a[i]=temp; }
//COUNTING INVERSIONS -- THIS PART CAN BE REPLACED WITH WR
for (int i = 0; i a[j]) b++; } }
//OUTPUT cout
return 0;
}
Algorithm WR. (1) Intitialize Bi to 0. (2) For each even Alj] find elements with indices smaller than j that are by one larger than Al]: increase Bli] by the number of such elements; (3) Divide each Ali] by 2 (in the integer sense); (4) Stop when all Ali] are 0 Note that since we are only interested in the number of inversions, we do not need to store the entire vector B. Rather, we can accumulate the total number of pairs even/biy one larger to the left
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
