-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInversion_count.cpp
More file actions
45 lines (39 loc) · 784 Bytes
/
Copy pathInversion_count.cpp
File metadata and controls
45 lines (39 loc) · 784 Bytes
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
#include<iostream>
#include<cstdio>
using namespace std;
//int main()
//{
// int inv_count=0;
// //int a[]={1,20,6,4,5};
// int a[]={2,4,1,3,5};
// int N=5;
// for(int i=0;i<N;i++)
// {
// for(int j=0;j<N;j++)
// {
// if(a[i]>a[j] && i<j)
// inv_count++;
// }
// }
// cout<<inv_count;
//
// return 0;
//}
int getInvCount(int arr[], int n)
{
int inv_count = 0;
int i, j;
for(i = 0; i < n - 1; i++)
for(j = i+1; j < n; j++)
if(arr[i] > arr[j])
inv_count++;
return inv_count;
}
/* Driver progra to test above functions */
int main(int argv, char** args)
{
int arr[] = {1, 20, 6, 4, 5};
printf(" Number of inversions are %d \n", getInvCount(arr, 5));
getchar();
return 0;
}