-
Notifications
You must be signed in to change notification settings - Fork 0
/
inversionCountInCPP.cpp
54 lines (54 loc) · 1.71 KB
/
inversionCountInCPP.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Implemented by Kritagya Kumra
#include <iostream>
using namespace std;
int merge(int arr[], int temp[], int left, int mid, int right)
{
int i, j, k;
int count = 0;
i = left; // i to locate first array location
j = mid; // i to locate second array location
k = left; // i to locate merged array location
while ((i <= mid - 1) && (j <= right))
{
if (arr[i] <= arr[j])
{ // when left item is less than right item
temp[k++] = arr[i++];
}
else
{
temp[k++] = arr[j++];
count += (mid - i); // find how many convertion is performed
}
}
while (i <= mid - 1) // if first list has remaining item, add them in the list
temp[k++] = arr[i++];
while (j <= right) // if second list has remaining item, add them in the list
temp[k++] = arr[j++];
for (i = left; i <= right; i++)
arr[i] = temp[i]; // store temp Array to main array
return count;
}
int mergeSort(int arr[], int temp[], int left, int right)
{
int mid, count = 0;
if (right > left)
{
mid = (right + left) / 2; // find mid index of the array
count = mergeSort(arr, temp, left, mid); // merge sort left sub array
count += mergeSort(arr, temp, mid + 1, right); // merge sort right sub array
count += merge(arr, temp, left, mid + 1, right); // merge two sub arrays
}
return count;
}
int arrInversion(int arr[], int n)
{
int temp[100] = {0};
return mergeSort(arr, temp, 0, n - 1);
}
int main()
{
int arr[] = {4, 2, 1, 3, 5, 8, 0, 43, 10};
int n = 9;
cout << "Number of inversions are " << arrInversion(arr, n);
}
// Implemented by Kritagya Kumra