Measuring how an array is "out of order"

Given an array of values, I want to find a general “score”, where the rating of each element is the number of elements with a lower value that appears before it in the array.

eg.

values: 4 1 3 2 5
scores: 0 0 1 1 4
total score: 6

The O (n ^ 2) algorithm is trivial, but I suspect that it is possible to do this in O (nlgn), by sorting the array. Does anyone have any ideas how to do this, or if this is not possible?

+5
source share
2 answers

, , , , (.. ). O (n * log (n)), , . , , , ( ).

+5

, , , - , Fenwick Trees. Fenwick , . O (nlogm), n - , m - .

( ), MAK , :)

+2

All Articles