You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 04-Sorting/inversions.cpp
+8-1Lines changed: 8 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,8 @@
1
+
/*
2
+
Count Inversions in an array using both the O(n^2) solution and using the optimised O(nlogn) solution.
3
+
Inversion Count for an array indicates – how far (or close) the array is from being sorted. If the array is already sorted, then the inversion count is 0, but if the array is sorted in the reverse order, the inversion count is the maximum.
4
+
Formally speaking, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j
5
+
*/
1
6
#include"bits/stdc++.h"
2
7
usingnamespacestd;
3
8
voidprintArr(int arr[], int n,string s="\n"){
@@ -14,6 +19,7 @@ void printArr(int arr[], int n,string s="\n"){
14
19
}
15
20
}
16
21
intmerge(int left[], int right[], int arr[], int leftLength, int rightLength){
22
+
//the helper function which helps to merge two arrays, left and right
17
23
int i=0,j=0,k = 0;
18
24
int inversions = 0 ;
19
25
while(i< leftLength && j<rightLength){
@@ -44,6 +50,7 @@ int merge(int left[], int right[], int arr[], int leftLength, int rightLength){
44
50
return inversions;
45
51
}
46
52
intmergeSort(int arr[], int n){
53
+
//the main merge sort function takes in array and the number of elements in the array
0 commit comments