-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinversion.cpp
More file actions
60 lines (57 loc) · 878 Bytes
/
inversion.cpp
File metadata and controls
60 lines (57 loc) · 878 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//O(nlg(n))=============inversion
#include <iostream>
#include <limits.h>
using namespace std;
int number = 0;
//This function will cost O(n) time
void m_merge(int A[],int p,int q,int r1)
{
int n1 = q - p + 1;
int n2 = r1 - q;
int L[n1+1];
int R[n2+1];
for(int i = 0; i < n1; ++i)
{
L[i] = A[p+i];
}
for(int i = 0; i < n2; ++i)
{
R[i] = A[q+i+1];
}
L[n1] = INT_MAX;
R[n2] = INT_MAX;
int l = 0;
int r = 0;
for(int i = p; i <= r1; ++i)
{
if(L[l]<=R[r])
{
A[i] = L[l++];
}
else
{
A[i] = R[r++];
number += n1 - l;
}
}
}
void merge_sort(int A[],int p,int r)
{
if(p < r)
{
int q = (p+r)/2;
merge_sort(A,p,q);
merge_sort(A,q+1,r);
m_merge(A,p,q,r);
}
}
int main(int argc, char **argv) {
int s[]={2, 3, 8, 6, 1};
merge_sort(s,0,4);
for(int i = 0; i < 5; ++i)
{
cout << s[i] << " ";
}
cout << number <<endl;
return 0;
}