-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Count_Inversions.c
86 lines (77 loc) · 1.93 KB
/
Count_Inversions.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
83
84
85
/*
Problem Statement:
Given an array of integers. Find the Inversion Count in the array.
Two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j.
*/
#include <stdio.h>
// answer stores the inversion count.
int answer = 0;
// Merge function helps us to calculate the inversion count.
void Merge(int input[], int start, int end, int mid)
{
// temp is an array, which finally stores the elements of input array in sorted manner.
int temp[end - start + 1];
int left = start, right = mid + 1, new_ind = 0;
while (left <= mid && right <= end)
{
if (input[left] <= input[right])
{
temp[new_ind++] = input[left++];
}
else
{
// In this case, inversions are possible.
answer += (mid - left + 1);
temp[new_ind++] = input[right++];
}
}
while (left <= mid)
{
temp[new_ind++] = input[left++];
}
while (right <= end)
{
temp[new_ind++] = input[right++];
}
for (int index = start; index <= end; index++)
{
input[index] = temp[index - start];
}
return;
}
void MergeSort(int input[], int start, int end)
{
if (start >= end)
{
return;
}
int mid = start + (end - start) / 2;
MergeSort(input, start, mid);
MergeSort(input, mid + 1, end);
Merge(input, start, end, mid);
}
int main()
{
printf("Enter the size of the input array\n");
int SIZE;
scanf("%d", &SIZE);
int input[SIZE];
printf("Enter the elements of the input array\n");
for (int i = 0; i < SIZE; i++)
{
scanf("%d", &input[i]);
}
MergeSort(input, 0, SIZE - 1);
printf("Number of inversions in the input array are %d.", answer);
}
/*
Time Complexity : O(SIZE*log(SIZE)).
Space Complexity : O(SIZE).
Input:
Enter the size of the input array
5
Enter the elements of the input array
2 4 1 3 5
Output:
Number of inversions in the input array are 3.
*/