Skip to content

Commit 1f5ca99

Browse files
rakshitrajtjgurwara99Panquesito7github-actionspoyea
authored
feat: Add sorting/count_inversions.cpp (#1425)
* Create count_inversions.cpp Counts the number of inversions in a list using merge sort. The number of Inversions in a list is the measure of the list's proximity to being sorted in ascending/increasing order. * Update count_inversions.cpp - fixed template error on line 156 - an added test case for character array - an added test case for list type double * Update sorting/count_inversions.cpp Co-authored-by: Taj <[email protected]> * Update sorting/count_inversions.cpp Co-authored-by: Taj <[email protected]> * Update sorting/count_inversions.cpp Co-authored-by: David Leal <[email protected]> * updating DIRECTORY.md * clang-format and clang-tidy fixes for b51af5e * using `uint64_t` from cstdint header and doxygen formatiing * clang-format and clang-tidy fixes for 047578b * Update count_inversions.cpp * added comments on imports * clang-format and clang-tidy fixes for 2f65017 * Update sorting/count_inversions.cpp Co-authored-by: David Leal <[email protected]> * Update sorting/count_inversions.cpp Co-authored-by: David Leal <[email protected]> * Update sorting/count_inversions.cpp Co-authored-by: David Leal <[email protected]> * Update sorting/count_inversions.cpp Co-authored-by: David Leal <[email protected]> * Update count_inversions.cpp * clang-format and clang-tidy fixes for 5d5cc53 * Update sorting/count_inversions.cpp Co-authored-by: David Leal <[email protected]> * Update sorting/count_inversions.cpp Co-authored-by: David Leal <[email protected]> * Update sorting/count_inversions.cpp Co-authored-by: David Leal <[email protected]> * Update sorting/count_inversions.cpp Co-authored-by: David Leal <[email protected]> * Update sorting/count_inversions.cpp Co-authored-by: David Leal <[email protected]> * Update sorting/count_inversions.cpp Co-authored-by: David Leal <[email protected]> * Update sorting/count_inversions.cpp Co-authored-by: David Leal <[email protected]> * Update sorting/count_inversions.cpp Co-authored-by: David Leal <[email protected]> * Update sorting/count_inversions.cpp Co-authored-by: David Leal <[email protected]> * Update sorting/count_inversions.cpp Co-authored-by: David Leal <[email protected]> * Update sorting/count_inversions.cpp Co-authored-by: David Leal <[email protected]> * Update sorting/count_inversions.cpp Co-authored-by: David Leal <[email protected]> * Update count_inversions.cpp * Update sorting/count_inversions.cpp Co-authored-by: David Leal <[email protected]> * Update sorting/count_inversions.cpp Co-authored-by: David Leal <[email protected]> * Update sorting/count_inversions.cpp Co-authored-by: David Leal <[email protected]> * Update count_inversions.cpp * clang-format and clang-tidy fixes for 121ce33 * Update sorting/count_inversions.cpp Co-authored-by: John Law <[email protected]> Co-authored-by: Taj <[email protected]> Co-authored-by: David Leal <[email protected]> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: John Law <[email protected]>
1 parent 60c607b commit 1f5ca99

File tree

2 files changed

+276
-0
lines changed

2 files changed

+276
-0
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@
255255
* [Bucket Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/bucket_sort.cpp)
256256
* [Cocktail Selection Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/cocktail_selection_sort.cpp)
257257
* [Comb Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/comb_sort.cpp)
258+
* [Count Inversions](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/count_inversions.cpp)
258259
* [Counting Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/counting_sort.cpp)
259260
* [Counting Sort String](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/counting_sort_string.cpp)
260261
* [Cycle Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/cycle_sort.cpp)

sorting/count_inversions.cpp

+275
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
/**
2+
* @file
3+
* @brief Counting Inversions using [Merge
4+
Sort](https://en.wikipedia.org/wiki/Merge_sort)
5+
*
6+
* @details
7+
* Program to count the number of inversions in an array
8+
* using merge-sort.
9+
*
10+
* The count of inversions help to determine how close the array
11+
* is to be sorted in ASCENDING order.
12+
*
13+
* two elements a[i] and a[j] form an inversion if `a[i]` > `a[j]` and i < j
14+
*
15+
* Time Complexity --> `O(n.log n)`
16+
17+
* Space Complexity --> `O(n)` ; additional array `temp[1..n]`
18+
* ### Algorithm
19+
20+
* 1. The idea is similar to merge sort, divide the array into two equal or
21+
almost
22+
* equal halves in each step until the base case is reached.
23+
* 2. Create a function merge that counts the number of inversions when two
24+
halves of
25+
* the array are merged, create two indices i and j, i is the index for
26+
first half
27+
* and j is an index of the second half. if `a[i]` is greater than `a[j]`,
28+
then there are (mid – i)
29+
* inversions, Because left and right subarrays are sorted, so all the
30+
remaining elements
31+
* in left-subarray (a[i+1], a[i+2] … a[mid]) will be greater than a[j].
32+
* 3. Create a recursive function to divide the array into halves and find the
33+
answer by summing
34+
* the number of inversions is the first half, number of inversion in the
35+
second half and
36+
* the number of inversions by merging the two.
37+
* 4. The base case of recursion is when there is only one element in the
38+
given half.
39+
* 5. Print the answer
40+
*
41+
* @author [Rakshit Raj](https://github.com/rakshitraj)
42+
*/
43+
#include <cassert> /// for assert
44+
#include <cstdint> /// for typedef datatype uint64_t
45+
#include <iostream> /// for IO operations
46+
#include <vector> /// for std::vector
47+
48+
/**
49+
* @namespace sorting
50+
* @brief Sorting algorithms
51+
*/
52+
namespace sorting {
53+
/**
54+
* @namespace inversion
55+
* @brief Functions for counting inversions using Merge Sort algorithm
56+
*/
57+
namespace inversion {
58+
59+
// Functions used --->
60+
// int mergeSort(int* arr, int* temp, int left, int right);
61+
// int merge(int* arr, int* temp, int left, int mid, int right);
62+
// int countInversion(int* arr, const int size);
63+
// void show(int* arr, const int size);
64+
65+
/**
66+
* @brief Function to merge two sub-arrays.
67+
*
68+
* @details
69+
* merge() function is called from mergeSort()
70+
* to merge the array after it split for sorting
71+
* by the mergeSort() funtion.
72+
*
73+
* In this case the merge fuction will also count and return
74+
* inversions detected when merging the sub arrays.
75+
*
76+
* @param arr input array, data-menber of vector
77+
* @param temp stores the resultant merged array
78+
* @param left lower bound of `arr[]` and left-sub-array
79+
* @param mid midpoint, upper bound of left sub-array,
80+
* `(mid+1)` gives the lower bound of right-sub-array
81+
* @param right upper bound of `arr[]` and right-sub-array
82+
* @returns number of inversions found in merge step
83+
*/
84+
template <typename T>
85+
uint32_t merge(T* arr, T* temp, uint32_t left, uint32_t mid, uint32_t right) {
86+
uint32_t i = left; /* i --> index of left sub-array */
87+
uint32_t j = mid + 1; /* j --> index for right sub-array */
88+
uint32_t k = left; /* k --> index for resultant array temp */
89+
uint32_t inv_count = 0; // inversion count
90+
91+
while ((i <= mid) && (j <= right)) {
92+
if (arr[i] <= arr[j]) {
93+
temp[k++] = arr[i++];
94+
} else {
95+
temp[k++] = arr[j++];
96+
inv_count +=
97+
(mid - i +
98+
1); // tricky; may vary depending on selection of sub-array
99+
}
100+
}
101+
// Add remaining elements from the larger subarray to the end of temp
102+
while (i <= mid) {
103+
temp[k++] = arr[i++];
104+
}
105+
while (j <= right) {
106+
temp[k++] = arr[j++];
107+
}
108+
// Copy temp[] to arr[]
109+
for (k = left; k <= right; k++) {
110+
arr[k] = temp[k];
111+
}
112+
return inv_count;
113+
}
114+
115+
/**
116+
* @brief Implement merge Sort and count inverions while merging
117+
*
118+
* @details
119+
* The mergeSort() function implements Merge Sort, a
120+
* Divide and conquer algorithm, it divides the input
121+
* array into two halves and calls itself for each
122+
* sub-array and then calls the merge() function to
123+
* merge the two halves.
124+
*
125+
* @param arr - array to be sorted
126+
* @param temp - merged resultant array
127+
* @param left - lower bound of array
128+
* @param right - upper bound of array
129+
* @returns number of inversions in array
130+
*/
131+
template <typename T>
132+
uint32_t mergeSort(T* arr, T* temp, uint32_t left, uint32_t right) {
133+
uint32_t mid = 0, inv_count = 0;
134+
if (right > left) {
135+
// midpoint to split the array
136+
mid = (right + left) / 2;
137+
// Add inversions in left and right sub-arrays
138+
inv_count += mergeSort(arr, temp, left, mid); // left sub-array
139+
inv_count += mergeSort(arr, temp, mid + 1, right);
140+
141+
// inversions in the merge step
142+
inv_count += merge(arr, temp, left, mid, right);
143+
}
144+
return inv_count;
145+
}
146+
147+
/**
148+
* @brief Function countInversion() returns the number of inversion
149+
* present in the input array. Inversions are an estimate of
150+
* how close or far off the array is to being sorted.
151+
*
152+
* @details
153+
* Number of inversions in a sorted array is 0.
154+
* Number of inversion in an array[1...n] sorted in
155+
* non-ascending order is n(n-1)/2, since each pair of elements
156+
* contitute an inversion.
157+
*
158+
* @param arr - array, data member of std::vector<int>, input for counting
159+
* inversions
160+
* @param array_size - number of elementa in the array
161+
* @returns number of inversions in input array, sorts the array
162+
*/
163+
template <class T>
164+
uint32_t countInversion(T* arr, const uint32_t size) {
165+
std::vector<T> temp;
166+
temp.reserve(size);
167+
temp.assign(size, 0);
168+
return mergeSort(arr, temp.data(), 0, size - 1);
169+
}
170+
171+
/**
172+
* @brief UTILITY function to print array.
173+
* @param arr[] array to print
174+
* @param array_size size of input array arr[]
175+
* @returns void
176+
*
177+
*/
178+
template <typename T>
179+
void show(T* arr, const uint32_t array_size) {
180+
std::cout << "Printing array: \n";
181+
for (uint32_t i = 0; i < array_size; i++) {
182+
std::cout << " " << arr[i];
183+
}
184+
std::cout << "\n";
185+
}
186+
187+
} // namespace inversion
188+
} // namespace sorting
189+
190+
/**
191+
* @brief Test implementations
192+
* @returns void
193+
*/
194+
static void test() {
195+
// Test 1
196+
std::vector<uint64_t> arr1 = {
197+
100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84,
198+
83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67,
199+
66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50,
200+
49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33,
201+
32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
202+
15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
203+
uint32_t size1 = arr1.size();
204+
uint32_t inv_count1 = 4950;
205+
uint32_t result1 = sorting::inversion::countInversion(arr1.data(), size1);
206+
assert(inv_count1 == result1);
207+
// Test 2
208+
std::vector<int> arr2 = {22, 66, 75, 23, 11, 87, 2, 44, 98, 43};
209+
uint32_t size2 = arr2.size();
210+
uint32_t inv_count2 = 20;
211+
uint32_t result2 = sorting::inversion::countInversion(arr2.data(), size2);
212+
assert(inv_count2 == result2);
213+
// Test 3
214+
std::vector<double> arr3 = {33.1, 45.2, 65.4, 76.5, 1.0,
215+
2.9, 5.4, 7.7, 88.9, 12.4};
216+
uint32_t size3 = arr3.size();
217+
uint32_t inv_count3 = 21;
218+
uint32_t result3 = sorting::inversion::countInversion(arr3.data(), size3);
219+
assert(inv_count3 == result3);
220+
// Test 4
221+
std::vector<char> arr4 = {'a', 'b', 'c', 'd', 'e'};
222+
uint32_t size4 = arr4.size();
223+
uint32_t inv_count4 = 0;
224+
uint32_t result4 = sorting::inversion::countInversion(arr4.data(), size4);
225+
assert(inv_count4 == result4);
226+
}
227+
228+
// /**
229+
// * @brief Program Body contains all main funtionality
230+
// * @returns void
231+
// */
232+
// template <typename T>
233+
// static void body() {
234+
// // Input your own sequence
235+
// uint_t size;
236+
// T input;
237+
// std::cout << "Enter number of elements:";
238+
// std::cin >> size;
239+
//
240+
// std::vector<T> arr;
241+
// arr.reserve(size);
242+
//
243+
// std::cout << "Enter elements -->\n";
244+
// for (uint64_t i=1; i<=size; i++) {
245+
// std::cout << "Element "<< i <<" :";
246+
// std::cin >> input;
247+
// arr.push_back(input);
248+
// }
249+
//
250+
// if (size != arr.size()) {
251+
// size = arr.size();
252+
// }
253+
//
254+
// std::cout << "\n";
255+
// sorting::inversion::show(arr.data(), size);
256+
// std::cout << "\n";
257+
//
258+
// // Counting inversions
259+
// std::cout << "\nThe number of inversions: "<<
260+
// sorting::inversion::countInversion(arr.data(), size) << "\n";
261+
//
262+
// // Output sorted array
263+
// std::cout << "\nSorted array --> \n";
264+
// sorting::inversion::show(arr.data(), size);
265+
// }
266+
267+
/**
268+
* @brief Main function
269+
* @returns 0 on exit
270+
*/
271+
int main() {
272+
test(); // Run test implementations
273+
// body(); // test your own array
274+
return 0;
275+
}

0 commit comments

Comments
 (0)