-
Notifications
You must be signed in to change notification settings - Fork 7
/
Count_Inversions_MergeSort.cc
59 lines (45 loc) · 1.22 KB
/
Count_Inversions_MergeSort.cc
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
int count_inversion_merge(int array[], int first, int last)
{
int mid = (first+last)/2;
int ai = first;
int bi = mid+1;
int final[last-first+1], finali=0;
int inversion = 0, i;
while (ai <= mid && bi <= last) {
if (array[ai] <= array[bi]) {
final[finali++] = array[ai++];
} else {
final[finali++] = array[bi++];
inversion += mid - ai + 1;
}
}
while (ai <= mid)
final[finali++] = array[ai++];
while (bi <= last)
final[finali++] = array[bi++];
for (i=0 ; i<last-first+1 ; i++)
array[i+first] = final[i];
return inversion;
}
int count_inversion(int array[], int a, int b)
{
int x, y, z, mid;
if (a >= b) return 0;
mid = (a+b)/2;
x = count_inversion(array, a, mid);
y = count_inversion(array, mid+1, b);
z = count_inversion_merge(array, a, b);
return x+y+z;
}
int main()
{
int a[] = {9, 8 , 7, 6, 5, 4, 3, 2, 1, 10};
int len = 10;
int inversion = count_inversion(a, 0, len-1);
int i;
for (i=0 ; i<len; i++)
printf("%d ", a[i]);
printf("\n");
printf("Total inversions : %d \n", inversion);
return 0;
}