Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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)

image text in transcribed

#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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning C# 5.0 Databases

Authors: Vidya Vrat Agarwal

2nd Edition

1430242604, 978-1430242604

More Books

Students also viewed these Databases questions