-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInversionCount.cpp
100 lines (96 loc) · 2.47 KB
/
InversionCount.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Implemented by Kritagya Kumra
#include <iostream>
using namespace std;
long Merge(long *arr, long start, long end)
{
long ans = 0;
long mid = start + (end - start) / 2;
long length1 = mid - start + 1;
long length2 = end - mid;
long *arr1 = new long[length1];
long *arr2 = new long[length2];
// Copy values of the arrays longo 2 different arrays and then
// we would combine those
long mainIndex = start;
for (long i = 0; i < length1; i++)
{
arr1[i] = arr[mainIndex++];
}
mainIndex = mid + 1;
for (long i = 0; i < length2; i++)
{
arr2[i] = arr[mainIndex++];
}
// Printing the arrays
cout << endl;
cout << "Printing the arrays1" << endl;
for (long i = 0; i < length1; i++)
{
cout << arr1[i] << " ";
}
cout << endl;
cout << "Printing the arrays2" << endl;
for (long i = 0; i < length2; i++)
{
cout << arr2[i] << " ";
}
// Merge two sorted arrays using the index
long ArrayIndex1 = 0;
long ArrayIndex2 = 0;
mainIndex = start;
while (ArrayIndex1 < length1 && ArrayIndex2 < length2)
{
// Choose the smaller of the two values and then add it to the main array
if (arr1[ArrayIndex1] > arr2[ArrayIndex2])
{
ans += 1;
arr[mainIndex++] = arr1[ArrayIndex1++];
}
else
{
arr[mainIndex++] = arr2[ArrayIndex2++];
}
}
// Check for the elements left if one array is greater than the other elements
while (ArrayIndex1 < length1)
{
// if left array has more elements
arr[mainIndex++] = arr1[ArrayIndex1++];
}
while (ArrayIndex2 < length2)
{
// if right array has more elements
arr[mainIndex++] = arr2[ArrayIndex2++];
}
delete[] arr1;
delete[] arr2;
return ans;
}
long InversionCount(long *arr, long start, long end)
{
// Base case
if (start >= end)
{
return 0;
}
long mid = start + (end - start) / 2;
long ans = 0;
// Sort the left half
InversionCount(arr, start, mid);
// Sort the right half
InversionCount(arr, mid + 1, end);
// Merge two sorted arrays
ans += Merge(arr, start, end);
return ans;
}
int main()
{
long arr[10] = {23, 1, 13, 53, 45, 21, 5, 3, 55, 8};
long size = 10;
cout << InversionCount(arr, 0, size) << endl;
for (long i = 0; i < 10; i++)
{
cout << arr[i] << " ";
}
}
// Implemented by Kritagya Kumra