forked from sinagarajan/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindCountingInversionInArray.cpp
More file actions
89 lines (68 loc) · 2.2 KB
/
FindCountingInversionInArray.cpp
File metadata and controls
89 lines (68 loc) · 2.2 KB
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
86
87
88
89
/*
Find number of inversions in the array . Example : {1,3,4,2} . Right element < Left Element . In this case 2 is less than 3 and 4 and hence inversions count=2
Using the merge sort find the inversions . [ Whenever right hand side is less, all the remaning elements in left contribute to the inversion count
Algorithm :
1. Do the merge sort as usual
2. During the conquer steps, if any element in left is greater than element in right, then all element from left to mid will add to the inversion count
*/
# include <iostream>
using namespace std;
/* Merge function uses temp array that sorts all chracters from left to right */
int merge(int *array,int left, int mid,int right)
{
int inv=0;
int temp[right-left+1];
int pos=0,lpos=left,rpos=mid+1;
while(lpos <=mid && rpos <= right)
{
if( array[lpos] < array[rpos])
{
temp[pos]=array[lpos];
lpos++;
}
else
{
temp[pos]=array[rpos];
rpos++;
/* In case {1,3,4} and {2} , it has two inversions */
inv+= (mid-lpos+1);
}
pos++;
}
/* Fill the remaining elemets */
while(lpos<=mid)
{
temp[pos++]=array[lpos++];
}
while(rpos<=right)
{
temp[pos++]=array[rpos++];
}
int iter;
/* Copy back the sorted array to the original array */
for(iter = 0;iter < pos; iter++)
{
array[iter+left] = temp[iter];
}
return inv;
}
/* Merge functions that divides and call the conquers */
int mergesort(int arr[],int left,int right)
{
int inv=0;
int mid=(left+right)/2;
if(right > left)
{
inv=mergesort(arr,left,mid);
inv+=mergesort(arr,mid+1,right);
inv+=merge(arr,left,mid,right);
}
return inv;
}
int main()
{
int arr[]={5,4,3,2,1};
printf("Count is %d",mergesort(arr,0,4));
for(int i=0;i<5;i++)
printf("\t%d",arr[i]);
}