I have read something in the site that inversion means if i<j
then A[i]>A[j]
and it has some exercises about this , I have a lot of questions but I want to ask just one of them at first and then i will do the other exercises by myself if I can!!
Exercise: What permuta开发者_高级运维tion array (1,2, ..., n) has the highest number of inversion? What are these? thanks
Clearly N, ..., 2, 1
has the highest number of inversions. Every pair is an inversion. For example for N = 6
, we have 6 5 4 3 2 1
. The inversions are 6-5, 6-4, 6-3, 6-2, 6-1, 5-4, 5-3
and so on. Their number is N * (N - 1) / 2
.
Well, the identity permutation (1,2,...,n) has no inversions. Since an inversion is a pair of elements that are in reverse order than their indices, the answer probably involves some reversal of that permutation.
I have never heard the term inversion used in this way.
A decreasing array of length N, for N>0, has 1/2*N*(N-1) pairs i<j with A[i]>A[j]. This is the maximum possible.
精彩评论