Skip to content

Commit b9f4470

Browse files
authored
Update adaptive_merge_sort.cpp
i have documented the header added tparm added test for negative numbers
1 parent 387dd0d commit b9f4470

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

sorting/adaptive_merge_sort.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* @details This file contains the implementation of the Adaptive Merge Sort algorithm.
66
* The algorithm is a stable, divide-and-conquer based sorting algorithm.
77
*
8+
* @tparam T Type of elements to be sorted.
89
* @see https://medium.com/@kaweendra/the-ultimate-sorting-algorithm-6513d6968420
910
*/
1011

@@ -16,6 +17,7 @@
1617

1718
/**
1819
* @brief Function to merge two halves of an array
20+
* @tparam T Type of elements to be sorted
1921
* @param array Vector of elements to be sorted
2022
* @param aux Auxiliary vector used for merging
2123
* @param low Starting index
@@ -25,7 +27,6 @@
2527
template <typename T>
2628
void merge(std::vector<T>& array, std::vector<T>& aux, int low, int mid, int high) {
2729
std::copy(array.begin() + low, array.begin() + high + 1, aux.begin() + low);
28-
2930
int i = low;
3031
int j = mid + 1;
3132
for (int k = low; k <= high; ++k) {
@@ -43,6 +44,7 @@ void merge(std::vector<T>& array, std::vector<T>& aux, int low, int mid, int hig
4344

4445
/**
4546
* @brief Function to recursively sort an array using Adaptive Merge Sort
47+
* @tparam T Type of elements to be sorted
4648
* @param array Vector of elements to be sorted
4749
* @param aux Auxiliary vector used for merging
4850
* @param low Starting index
@@ -51,7 +53,6 @@ void merge(std::vector<T>& array, std::vector<T>& aux, int low, int mid, int hig
5153
template <typename T>
5254
void sort(std::vector<T>& array, std::vector<T>& aux, int low, int high) {
5355
if (low >= high) return;
54-
5556
int mid = low + (high - low) / 2;
5657
sort(array, aux, low, mid);
5758
sort(array, aux, mid + 1, high);
@@ -60,13 +61,13 @@ void sort(std::vector<T>& array, std::vector<T>& aux, int low, int high) {
6061

6162
/**
6263
* @brief Function to sort an array using Adaptive Merge Sort algorithm
64+
* @tparam T Type of elements to be sorted
6365
* @param array Vector of elements to be sorted
6466
* @return Vector of sorted elements
6567
*/
6668
template <typename T>
6769
std::vector<T> adaptiveMergeSort(std::vector<T> array) {
6870
if (array.size() <= 1) return array;
69-
7071
std::vector<T> aux(array.size());
7172
sort(array, aux, 0, array.size() - 1);
7273
return array;
@@ -100,6 +101,11 @@ void testAdaptiveMergeSort() {
100101
std::vector<int> single = {42};
101102
std::vector<int> expectedSingle = {42};
102103
assert(adaptiveMergeSort(single) == expectedSingle);
104+
105+
// Test negative numbers
106+
std::vector<int> negatives = {-3, -1, -7, -4, -6, -2, -5};
107+
std::vector<int> expectedNegatives = {-7, -6, -5, -4, -3, -2, -1};
108+
assert(adaptiveMergeSort(negatives) == expectedNegatives);
103109

104110
std::cout << "All AdaptiveMergeSort tests passed!" << std::endl;
105111
}

0 commit comments

Comments
 (0)