We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
C++ solution. Solving this by merge sort is probably the quickest way, as it is done in O(nlogn) time, as opposed to other methods most of which run in O(n^2).
An addition to the merge sort algorithm made specifically for this question is that we need to keep track of the number of swaps made in the algorithm.
Merge Sort: Counting Inversions
You are viewing a single comment's thread. Return to all comments →
C++ solution. Solving this by merge sort is probably the quickest way, as it is done in O(nlogn) time, as opposed to other methods most of which run in O(n^2).
An addition to the merge sort algorithm made specifically for this question is that we need to keep track of the number of swaps made in the algorithm.