-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeSort_InversionCount.cpp
86 lines (65 loc) · 1.86 KB
/
mergeSort_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
#include<iostream>
using namespace std;
// Function to merge two sorted arrays and count inversions
int merge(int *arr, int s, int e) {
int mid = (s + e) / 2;
int len1 = mid - s + 1;
int len2 = e - mid;
int *first = new int[len1];
int *second = new int[len2];
// Copy values to temporary arrays
int mainArrayIndex = s;
for(int i = 0; i < len1; i++) {
first[i] = arr[mainArrayIndex++];
}
mainArrayIndex = mid + 1;
for(int i = 0; i < len2; i++) {
second[i] = arr[mainArrayIndex++];
}
// Merge two sorted arrays and count inversions
int index1 = 0;
int index2 = 0;
mainArrayIndex = s;
int invCount = 0;
while(index1 < len1 && index2 < len2) {
if(first[index1] <= second[index2]) {
arr[mainArrayIndex++] = first[index1++];
} else {
arr[mainArrayIndex++] = second[index2++];
invCount += (len1 - index1); // Count inversions
}
}
while(index1 < len1) {
arr[mainArrayIndex++] = first[index1++];
}
while(index2 < len2) {
arr[mainArrayIndex++] = second[index2++];
}
delete []first;
delete []second;
return invCount;
}
// Function to perform merge sort and count inversions
int mergeSort(int *arr, int s, int e) {
if(s >= e) {
return 0;
}
int mid = (s + e) / 2;
int invCount = 0;
invCount += mergeSort(arr, s, mid);
invCount += mergeSort(arr, mid + 1, e);
invCount += merge(arr, s, e);
return invCount;
}
int main() {
int arr[15] = {3, 7, 0, 1, 5, 8, 3, 2, 34, 66, 87, 23, 12, 12, 12};
int n = 15;
int inversionCount = mergeSort(arr, 0, n - 1);
cout << "Sorted array: ";
for(int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
cout << "Number of inversions: " << inversionCount << endl;
return 0;
}