From e9d6bef403fc416ee809b0d7ea2e1dbf9193055c Mon Sep 17 00:00:00 2001 From: Yassin Bahloul Date: Mon, 15 Oct 2018 19:32:06 +0200 Subject: [PATCH 1/2] Add function counting the number of inversions based on merge sort. --- Sorting/MergeSort/cpp/CountInversions.cpp | 44 +++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Sorting/MergeSort/cpp/CountInversions.cpp diff --git a/Sorting/MergeSort/cpp/CountInversions.cpp b/Sorting/MergeSort/cpp/CountInversions.cpp new file mode 100644 index 00000000..3d06bae2 --- /dev/null +++ b/Sorting/MergeSort/cpp/CountInversions.cpp @@ -0,0 +1,44 @@ +#include +#include +#include + +using namespace std; + +long long count_inversions(int *a, int n) { + if (n <= 1) { + return 0; + } + int sz1 = n / 2, sz2 = n - sz1; + + // get count of inversions in left and right half + long long res = count_inversions(a, sz1) + count_inversions(a + sz1, sz2); + + // merge both halves (now copied into t1, resp. t2) into a + vector t1(a, a + sz1), t2(a + sz1, a + n); + int k = 0, i = 0, j = 0; + while (i < sz1 && j < sz2) { + if (t1[i] <= t2[j]) { + a[k++] = t1[i++]; + + // all elements in t2[0..j-1] (which are j elements) are smaller than t1[i] + // and are to the right of t1[i] in the original array --> form an inversion + res += j; + } else { + a[k++] = t2[j++]; + } + } + while (i < sz1) { + a[k++] = t1[i++]; + res += j; // j == sz2 holds here + } + while (j < sz2) { + a[k++] = t2[j++]; + } + return res; +} + +int main() { + vector v{10, 10, 20, 30, 12, 8, 9, 14, 13, 11}; + cout << "Given array has " << count_inversions(&v[0], v.size()) << " inversions.\n"; + return 0; +} \ No newline at end of file From c077b931001d141ed8c1f08213a509ebb6fcadf3 Mon Sep 17 00:00:00 2001 From: Yassin Bahloul Date: Mon, 15 Oct 2018 19:34:31 +0200 Subject: [PATCH 2/2] Remove unused import. --- Sorting/MergeSort/cpp/CountInversions.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/Sorting/MergeSort/cpp/CountInversions.cpp b/Sorting/MergeSort/cpp/CountInversions.cpp index 3d06bae2..39fae006 100644 --- a/Sorting/MergeSort/cpp/CountInversions.cpp +++ b/Sorting/MergeSort/cpp/CountInversions.cpp @@ -1,6 +1,5 @@ #include #include -#include using namespace std;