-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.c
82 lines (63 loc) · 1.82 KB
/
4.c
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
#include <stdio.h>
long long merge(int arr[], int left[], int leftSize, int right[], int rightSize) {
int i = 0, j = 0, k = 0;
long long invCount = 0;
while (i < leftSize && j < rightSize) {
if (left[i] <= right[j]) {
arr[k] = left[i];
i++;
} else {
arr[k] = right[j];
j++;
invCount += leftSize - i; // Increment invCount by the number of remaining elements in the left subarray
}
k++;
}
while (i < leftSize) {
arr[k] = left[i];
i++;
k++;
}
while (j < rightSize) {
arr[k] = right[j];
j++;
k++;
}
return invCount;
}
long long mergeSort(int arr[], int size) {
if (size < 2) {
return 0;
}
int mid = size / 2;
int left[mid];
int right[size - mid];
for (int i = 0; i < mid; i++) {
left[i] = arr[i];
}
for (int i = mid; i < size; i++) {
right[i - mid] = arr[i];
}
long long invCount = 0;
invCount += mergeSort(left, mid); // Count inversions in left subarray
invCount += mergeSort(right, size - mid); // Count inversions in right subarray
invCount += merge(arr, left, mid, right, size - mid); // Merge two sorted subarrays and count inversions
return invCount;
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {9, 5, 1, 3, 8, 4, 2, 7, 6};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, size);
long long inversions = mergeSort(arr, size);
printf("Sorted array: ");
printArray(arr, size);
printf("Number of inversions: %lld\n", inversions);
return 0;
}