Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Count_Inversion.cpp #2625

Merged
merged 1 commit into from
Apr 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 */