366 views

2 Answers

Best answer
0 votes
0 votes

for element n inversion would be...n-1,n-2,n-3,.....1 

for n-1  it would be n-2,n-3,n-4......1 and so on...

so total would be...

n-1+n-2+n-3+n-4+.....+1+0 =(n-1)(n-1+1)/2 ->n(n-1)/2

selected by
3 votes
3 votes

$Inversions:$

a[i] and a[j] form an inversion if a[i] > a[j] and i < j 

Hence  2, 4, 1, 3, 5 has three inversions (2, 1), (4, 1), (4, 3)

I can prove the maximum and average number of inversions in an array easily, like so...

The max will be when the array is fully unsorted in which case the number of inversions would be 

(n−1)+(n−2)+(n−3)........(n-(n-1))

This gets simplified to $0.5(n-1)n  = \frac{(n*(n-1))}{2}$ (worst case)

For average case 

 Now the average number of inversions will just be halfway between the best and worst case scenario. The worst case scenario is 0.5(n−1)n and the best case scenario is 0 so the

average would be $0.25(n−1)n = \frac{(n*(n-1))}{4}$

If you want to proof it by induction you can take help from this source: https://math.stackexchange.com/questions/905700/maximum-and-average-number-of-inversions-in-array-by-induction


 

Related questions

0 votes
0 votes
1 answer
1
deepak_8404 asked Oct 1, 2023
294 views
Consider a lower triangular matrix stored in row major order as p[-25 - - + 749][-25 - - - + 749] with base address = 6800, size of each element = 6 byte. Find the value ...
–4 votes
–4 votes
2 answers
3
Souvik33 asked Oct 27, 2022
649 views
*MSQ*The following figure depicts a a. A tree and only treeb. A tree with 3 nodesc. A graph (Since every tree is a graph)d. A graph and only graph
0 votes
0 votes
1 answer
4
Overflow04 asked Sep 28, 2022
453 views
Can we determine unique tree by Inorder and level order traversal .