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)

 i need C++ code for the following algorithm so i can

#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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!