Javatpoint Logo
Javatpoint Logo

Inversion count

What is inversion count?

The inversion count concept is used in the array and can be performed using an array data structure. In inversion count, we will specify how we are required to sort an array. We all need to find a couple of elements, for which the first element is always greater than the second one.

  • If an array is already sorted, the total inversion count is 0.
  • But if an array is sorted in descending order, then the total inversion count is maximum because we will be able to get the maximum couple of elements which is present in the array.
  • For identifying the correct pairs, we will traverse the array, and then find the element present at ith index and compare it with (i + 1)th index; if it is found to be greater than the next index, then we will form the pair of it and increase the number of inversion count by one.
  • In this way, we will traverse the whole array starting from the 0th index towards the (n-1)th index and increase the value of the inversion count if we find the correct pairs according to the above-specified condition.

Let us try to understand it with the help of an example -

We are given an array.

Example 1 :

Given:

A = { 10 , 1 , 2 , 4 , 13 , 9 , 5 }

Output :

In the above example, you have to find the total number of inversions starting from the 0th index and upto the last index by comparing each value with another value.

And the number of inversions that are possible as follows: { ( 10 , 1 ) , ( 10 , 2 ) , ( 10 , 4 ) , ( 10 , 9 ) , ( 10 , 5 ) , ( 13 , 9 ) , ( 13 , 5 ) , ( 9 , 5 ) }

Total count of inversions are : 8

Example 2 :

Given:

A = { 1 , 5 , 2 , 4 , 13 , 19 , 15 }

Output :

In the above example, you have to find the total number of inversions starting from the 0th index and upto the last index by comparing each value with another value.

And the number of inversions that are possible as follows: { ( 5 , 2 ) , ( 5 , 4 ) , ( 19 , 15 ) }

Total count of inversions are : 3

Example 3 :

Given:

A = { 7 , 5 , 12 , 4 , 1 , 9 , 15 , 3 , 8 }

Output :

In the above example, find the total number of inversions starting from the 0th index and upto the last index by comparing each value with another value.

And the number of inversions that are possible as follows: { ( 7 , 5 ) , ( 7 , 4 ) , ( 7 , 1 ) , ( 7 , 3 ) , ( 5 , 4 ) , ( 5 , 1 ) , ( 5 , 3 ) , ( 12 , 4 ) , ( 12 , 1 ) , ( 12 , 9 ) , ( 12 , 3 ) , ( 12 , 8 ) , ( 4 , 1 ) , ( 4 , 3 ) , ( 15 , 3 ) , ( 15 , 8 ) }

Total count of inversions are : 16

Example 4 :

Given:

A = { 17 , 15 , 12 , 11 , 10 , 9 , 8 , 3 , 1 }

Output :

In the above example, find the total number of inversions starting from the 0th index and upto the last index by comparing each value with another value.

And the number of inversions that are possible as follows: { ( 17 , 15 ) , ( 17 , 12 ) , ( 17 , 11 ) , ( 17 , 10 ) , ( 17 , 9 ) , ( 17 , 8 ) , ( 17 , 3 ) , ( 17 , 1 ) , ( 15 , 12 ) , ( 15 , 11 ) , ( 15 , 10 ) , ( 15 , 9 ) , ( 15 , 8 ) , ( 15 , 3 ) , ( 15 , 1 ) , ( 12 , 11 ) , ( 12 , 10 ) , ( 12 , 9 ) , ( 12 , 8 ) , ( 12 , 3 ) , ( 12 , 1 ) , ( 11 , 10 ) , ( 11 , 9 ) , ( 11 , 8 ) , ( 11 , 3 ) , ( 11 , 1 ) , ( 10 , 9 ) , ( 10 , 8 ) , ( 10 , 3 ) , ( 10 , 1 ) , ( 9 , 8 ) , ( 9 , 3 ) , ( 9 , 1 ) , ( 8 , 3 ) , ( 8 , 1 ) , ( 3 , 1 ) }

The total count of inversions are: 36

Here if we observe, we have a sorted array in descending order, and we here we got the maximum inversion counts because each element is greater than its next element in the array.

Example 5 :

Given:

A = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 }

Output :

In the above example, you have to find the total number of inversions starting from the 0th index and upto the last index by comparing each value with another value.

And the number of inversions that are possible is as follows: { }

The total count of inversions are : 0

Here, we observe we have a sorted array in ascending order, and we got the minimum inversion counts, i.e., 0, because each element is smaller than its next element in the array.

Example 6 :

Given:

A = {17 , 5 , 12 , 4 , 1 , 9 , 3 , 8}

Output :

In the above example, find the total number of inversions starting from the 0th index and upto the last index by comparing each value with another value.

And the number of inversions that are possible as follows: { ( 17 , 5 ) , ( 17 , 12 ) , ( 17 , 4 ) , ( 17 , 1 ) , ( 17 , 9 ) , ( 17 , 3 ) , ( 17 , 8 ) , ( 5 , 4 ) , ( 5 , 1 ) , ( 5 , 3 ) , ( 12 , 4 ) , ( 12 , 1 ) , ( 12 , 9 ) , ( 12 , 3 ) , ( 12 , 8 ) , ( 4 , 1 ) , ( 4 , 3 ) , ( 9 , 3 ) , ( 9 , 8 ) }

The total count of inversions are : 19

Now, let us move further, as we have already seen that that is what we are going to do in this problem. Using the various programming languages will only display the number of inversions possible, only the inversion count value.

Methods used to solve the above inversion count problem

  1. Simple method
  2. By using Merge sort

Let us discuss these methods one by one in detail, and we will analyze each method's time and space complexity to compare the better among both methods.

Using a simple method to solve the inversion count problem

In this method, we will use the simple approach; first, we will do traverse the whole array from the starting index to the last index and then compare the elements of the right side element with every other element if the current element is found to be greater than the other element then we will increase the count of inversions. This process will continue until we check the last element of the given array. Finally, we will print the total number of inversions are present. For doing this, we will use the nested loop technique.

Let us discuss the algorithmic procedure to solve this problem practically and count the total number of inversions:

Algorithm to implement the above to solve inversion count problem -

Step 1 - Take an array from the user of ' n ' elements; elements refer to the non-negative integers in the main function. The array entered by the user can be in any order; it is not mandatory to enter in ascending or descending order.

Step 2 - Create a new function named inversion, which will take an array and the number of elements present in the array as an argument from the main function. It passes the original array into it and store the result produced from this function in some variable named as count in the main function. This function will return the total number of inversion counts found in the original main array.

Step 3 - In the inversion, function pass an array as an argument named as arr and the size of an array named as ' n ', after than declare some variable named as ' ic ' and initialized it with 0, it will denote the total number of inversions present in the array.

Step 4 - For counting the number of inversions, we are required to run two loops; one is nested inside another loop, the first loop will run from the index 0 to the n-1 index, and then another loop will run from the index j = i + 1 to n - 1, so to compare the value present at the ith index with every other value after to that index.

Step 5 - In the body of the nested loop, we have assigned a conditional statement, in which we will do compare the value present at the ith index with the value present at the jth index if the value at ith index is greater than that of the jth index than will increment the count of ' ic ' by 1, if not than we will check same for the next j index.

Step 6 - In the way mentioned above, we will traverse the whole array, check for every value till the end index, and finally return the ic as a result.

Step 7 - From the main function, we will print the value of a total number of inversion counts.

Implementation of Inversion count Problem using above Method in C Programming Language -

The output of the above program is:

Enter number of elements present in the array: 8
 Enter array: 10
8
1
9
4
12
20
13
  Printing the total number of inversions possible from the above array: 8 

Implementation of Inversion count Problem using above Method in C++ Programming Language -

The output of the above program is:

7
6
1
4
12
2
3
0
6
  Printing the total number of inversions possible from the above array: 30    

Implementation of Inversion count Problem using above Method in Java Programming Language -

The output of the above program is:

Enter number of elements present in the array: 9
 Enter array: 
19
7
16
1
4
1
2
3
6
  Printing the total number of inversions possible from the above array: 23    

Implementation of Inversion count Problem using above Method in Python Programming Language -

The output of the above program is:

The total number of inversions possible are: 27    

Implementation of Inversion count Problem using above Method in C# Programming Language -

The output of the above program is:

The total number of inversions possible are: 22    

As we have already seen the implementation of the inversion count problem using a simple method, now we will analyze the space and the time complexity of the above problem and in the above implementation method.

The time complexity of the above simple method for solving inversion count problem -

For analyzing the time complexity, we will check each line of the code and find the complexity of every line. Here, we require two loops; one is nested inside the other. For executing the first loop, we require n elements n time and similarly for nested loop we require n time it is in the nested formation hence the time complexity of both loops will be multiplied and finally we will get the time complexity as n2 with some constant.

T ( n ) = O ( n ).O ( n ) + C

T ( n ) = O ( n2 ) + C

Hence time complexity will be O ( n2 ), as we do the asymptotically rough idea that we require ' n2 ' time to solve the problem of ' n ' elements. Here C refers to some constant value.

Space Complexity of above simple Method for solving inversion count problem -

To find space complexity, we need to observe the program and analyze the space required to store all the variables; as we notice the program, we will observe that no extra space is required to store the elements.

S ( n ) = O ( 1 )

Using the merge sort method to solve the inversion count problem

In this method, we will use the concept of the merge sort; in merge basically, we will first break the array into multiple subarrays until we reach the base case, then we will apply the merge concept for combining the arrays by comparing the values of right subarray with the left subarray and finally merged them.

But in this, we will do some slightly change, in this partitioning of the array will be the same as we do in the merge sort, we will dive into the array until we get the single-sized subarray. After that we will merge them. In merging, we will count the total number of inversions present in the left side and similarly in the right side and maintain the count and combine in the original order and can easily count the total number of inversions possible at last. In this way, we will get the desired output. By using this method, we can reduce the time complexity as compared to the above method.

Let us discuss the algorithmic procedure to solve this problem practically and count the total number of inversions:

Algorithm to implement the above to solve inversion count problem -

Step 1 - Take an array from the user of ' n ' elements; elements refer to the non-negative integers in the main function. The array entered by the user can be in any order; it is not mandatory to enter in ascending or descending order.

Step 2 - Here, we must create two functions named merge_sort function, and the other is merge.

Step 3 - From the main function, we will call the MS function bypassing the original array and the size of the array as an argument in it.

Step 4 - The MS function will only take an array and the size of the array as an argument. Still, inside this function, we will create a dynamic array of the size same as the size passed in the argument; from it, we will then call the function named as merge_sort and pass the argument in it as an original array, a temporary array that we have created recently, the size of the array and the initial index, i.e., 0.

Step 5 - In the merge_sort function, we will have an array, size of array ' n ', left side, and the right side in the argument denoted as - merge_sort ( arr, n, l, r ). In this function, we will first check whether the left part is less than the right part or not, and if it is found to be true, we will find the mid by taking the average of the left and right values.

Step 6 - We will call the merge_sort internally first for l to the mid and second for mid + 1 to r. In this function, we will return the value of the inversion count; hence, we will store the value of the inversion count in ic and first initialize it with 0.

Step 7 - After calling the merge_sort function twice, we will call the merge function; the merge function will take an array, size of the array, left, mid and right part as an argument, and it will return the value of inversion count.

Step 8 - In the merge function, we will merge the two arrays and compare each value of one array with the other one, and if we get the value of the left subarray is greater than the right subarray, we will increase the count of inversion count.

Step 9 - Initially, in merge function, we will start the pointer i from index left and pointer j from mid, then we will bind up i and j in the while condition and run a loop from i is left to mid - 1 and j is mid to the right, then we will compare the value present at arr [ i ] with the arr [ j ] if we found arr [ i ] > arr [ j ] then we will increase the value of inversion count, which is initially initialized with 0, also in this, we will add one more factor, i.e., mid - 1, because as there are left and right subarrays are already sorted hence, we will get mid - 1 more inversion.

Step 10 - return the value of inversion count in the merge_sort function, and from the merge_sort function aggregate value of the inversion, the count is calculated and returned in the main function.

Step 11 - Finally, from the main function, we will print the value of a total number of inversion counts possible from the given array.

Implementation of Inversion count Problem using above Method in C Programming Language -

The output of the above program is:

Enter number of elements present in the array: 9
 Enter array: 
19
7
16
1
4
1
2
3
6
  Printing the total number of inversions possible from the above array: 23   

Implementation of Inversion count Problem using above Method in C++ Programming Language -

The output of the above program is:

Enter number of elements present in the array: 8
 Enter array: 10
8
1
9
4
12
20
13
  Printing the total number of inversions possible from the above array: 8   

Implementation of Inversion count Problem using above Method in JAVA Programming Language -

The output of the above program is:

Enter number of elements present in the array: 10
 Enter array: 
9
7
6
1
4
12
2
3
0
6
  The total number of inversion counts possible is: 30   

Implementation of Inversion count Problem using above Method in Python Programming Language -

The output of the above program is:

The total number of inversions possible from the above array is: 15   

Implementation of Inversion count Problem using above Method in C# Programming Language -

The output of the above program is:

The total number of inversions possible from the above array is: 22  

Implementation of Inversion count Problem using above Method in JavaScript Programming Language -

The output of the above program is:

The total number of inversions possible from the above array is: 5  

As we have already seen the implementation of the inversion count problem using a simple method, now we will analyze the space and the time complexity of the above problem and in the above implementation method.

The time complexity of the above simple method for solving inversion count problem -

For analyzing the time complexity, we will check each line of the code and find the complexity of every line. As a result of this using the merge sort method, the time complexity of the program will become n*log(n), because, here we are required n comparisons inside the loop and for merging the array after sorting, and log(n) complexity for calling the mergesort function in which we will call the same function twice at a time hence it will ultimately result in log(n) time.

T ( n ) = 2 T ( n / 2 ) + O ( n )

Finally the time complexity will be T ( n ) = O ( n * log ( n ) ).

Here we observe that we will come to know that the time complexity in all the three cases of Best case, worst case, and in the average case, the time complexity will remain the same, i.e., T ( n ) = O ( n * log ( n ) ).

Space Complexity of above simple Method for solving inversion count problem -

To find space complexity, we need to observe the program and analyze the space required to store all the variables; as we notice the program, we will observe that we require extra space for holding the temporary array of n elements.

S ( n ) = O ( n )







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA