1,726 views
1 votes
1 votes

We are given a sequence of n nos. a1, a2, a3,.......an, we will assume that all the nos. are distinct. We say two indices i < j form an inversion if ai > aj .

How much time will it take to find total no. of inversions in the given array?

a) O(n2)    b) O(nlogn)    c) O(n)   d) none of these

1 Answer

Best answer
5 votes
5 votes

Take array as: 4 1 5 3 2

Sorted: 1 2 3 4 5

Number of inversions:

for 1: 1 (how many numbers larger than 1 are on the left side of 1 in initial array)
for 2: 3
for 3: 2
for 4: 0
for 5: 0

So, total inversions = 1+3+2 = 6. 

An algorithm for finding this in $O(n \log n)$ is given in below link. 

https://www.cp.eng.chula.ac.th/~prabhas//teaching/algo/algo2008/count-inv.htm

edited by

Related questions

0 votes
0 votes
0 answers
2
0 votes
0 votes
1 answer
3
NeelParekh asked Jul 27, 2023
279 views
If an array is split in the form of increasing and decreasing order then what is TC to find minimum element in the array?
2 votes
2 votes
1 answer
4
h4kr asked Dec 30, 2022
473 views
What is the correct time complexity in $\theta()$ ?