Skip to content

Commit

Permalink
Count_Inversion.cpp (jainaman224#2625)
Browse files Browse the repository at this point in the history
  • Loading branch information
mansikagrawal authored and Mrunal committed Apr 8, 2020
1 parent dab5d59 commit 00165be
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions Count_Inversion/Count_Inversion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Count inversion using merge sort

#include <iostream>
using namespace std;

//merge two sorted arrays such that resultant array is also sorted
int merge(int array[], int aux[], int low, int mid, int high)
{
int x = low, y = mid, z = low, count = 0;

// checking till left and right half of merge sort
while ((x <= mid-1) && (y <= high))
{
if (array[x] <= array[y])
{
aux[z++] = array[x++];
}
else
{
aux[z++] = array[y++];
count += mid-x;
}
}

// Copy remaining elements
while (x <= mid-1)
{
aux[z++] = array[x++];
}

while (y <= high)
{
aux[z++] = array[y++];
}

// Sorting the original array with the help of aux array
for (int i = low; i <= high; i++)
{
array[i] = aux[i];
}
return count;
}

//merge sort to find inversion count
int mergeSort(int array[], int aux[], int low, int high)
{
int count = 0;
if (high > low)
{
int mid = (low + high) / 2;

// merge sort on Left half of the array
count = mergeSort(array, aux, low, mid);
// merge sort on Right half of the array
count += mergeSort(array, aux, mid + 1, high);
// Merge the two half
count += merge(array, aux, low, mid + 1, high);
}
return count;
}

//wrapper function that returns number of inversions
int inversions_count(int array[], int n)
{
int aux[n];
return mergeSort(array, aux, 0, n-1);
}

int main ()
{
int n;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++)
cin >> arr[i];
cout << (inversions_count(arr, n)) << endl;
return 0;
}

/* Sample input
5
1 20 6 4 5
Sample Output
5 */

0 comments on commit 00165be

Please sign in to comment.