-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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 #2442
Count_Inversion.cpp #2442
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add space around the << There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Space is already present mam There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. <<endl space is not present in it. |
||
return 0; | ||
} | ||
|
||
/* Sample input | ||
5 | ||
1 20 6 4 5 | ||
|
||
Sample Output | ||
5 */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add space around the operator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space is already present mam.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be for(int i = 0; i < n; i++)