Skip to content

Commit 387dd0d

Browse files
authored
Create adaptive_merge_sort.cpp
1 parent c2adc3b commit 387dd0d

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

sorting/adaptive_merge_sort.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* @file AdaptiveMergeSort.cpp
3+
* @brief Implementation of the Adaptive Merge Sort algorithm in C++.
4+
*
5+
* @details This file contains the implementation of the Adaptive Merge Sort algorithm.
6+
* The algorithm is a stable, divide-and-conquer based sorting algorithm.
7+
*
8+
* @see https://medium.com/@kaweendra/the-ultimate-sorting-algorithm-6513d6968420
9+
*/
10+
11+
#include <iostream>
12+
#include <vector>
13+
#include <algorithm>
14+
#include <cassert>
15+
#include <string>
16+
17+
/**
18+
* @brief Function to merge two halves of an array
19+
* @param array Vector of elements to be sorted
20+
* @param aux Auxiliary vector used for merging
21+
* @param low Starting index
22+
* @param mid Midpoint index
23+
* @param high Ending index
24+
*/
25+
template <typename T>
26+
void merge(std::vector<T>& array, std::vector<T>& aux, int low, int mid, int high) {
27+
std::copy(array.begin() + low, array.begin() + high + 1, aux.begin() + low);
28+
29+
int i = low;
30+
int j = mid + 1;
31+
for (int k = low; k <= high; ++k) {
32+
if (i > mid) {
33+
array[k] = aux[j++];
34+
} else if (j > high) {
35+
array[k] = aux[i++];
36+
} else if (aux[j] < aux[i]) {
37+
array[k] = aux[j++];
38+
} else {
39+
array[k] = aux[i++];
40+
}
41+
}
42+
}
43+
44+
/**
45+
* @brief Function to recursively sort an array using Adaptive Merge Sort
46+
* @param array Vector of elements to be sorted
47+
* @param aux Auxiliary vector used for merging
48+
* @param low Starting index
49+
* @param high Ending index
50+
*/
51+
template <typename T>
52+
void sort(std::vector<T>& array, std::vector<T>& aux, int low, int high) {
53+
if (low >= high) return;
54+
55+
int mid = low + (high - low) / 2;
56+
sort(array, aux, low, mid);
57+
sort(array, aux, mid + 1, high);
58+
merge(array, aux, low, mid, high);
59+
}
60+
61+
/**
62+
* @brief Function to sort an array using Adaptive Merge Sort algorithm
63+
* @param array Vector of elements to be sorted
64+
* @return Vector of sorted elements
65+
*/
66+
template <typename T>
67+
std::vector<T> adaptiveMergeSort(std::vector<T> array) {
68+
if (array.size() <= 1) return array;
69+
70+
std::vector<T> aux(array.size());
71+
sort(array, aux, 0, array.size() - 1);
72+
return array;
73+
}
74+
75+
/**
76+
* @brief Unit tests for the AdaptiveMergeSort algorithm
77+
*/
78+
void testAdaptiveMergeSort() {
79+
// Test Integer Input
80+
std::vector<int> integers = {4, 23, 6, 78, 1, 54, 231, 9, 12};
81+
std::vector<int> expectedIntegers = {1, 4, 6, 9, 12, 23, 54, 78, 231};
82+
assert(adaptiveMergeSort(integers) == expectedIntegers);
83+
84+
// Test String Input
85+
std::vector<std::string> strings = {"c", "a", "e", "b", "d"};
86+
std::vector<std::string> expectedStrings = {"a", "b", "c", "d", "e"};
87+
assert(adaptiveMergeSort(strings) == expectedStrings);
88+
89+
// Test with duplicates
90+
std::vector<int> duplicates = {1, 3, 2, 2, 5, 4};
91+
std::vector<int> expectedDuplicates = {1, 2, 2, 3, 4, 5};
92+
assert(adaptiveMergeSort(duplicates) == expectedDuplicates);
93+
94+
// Test empty array
95+
std::vector<int> empty = {};
96+
std::vector<int> expectedEmpty = {};
97+
assert(adaptiveMergeSort(empty) == expectedEmpty);
98+
99+
// Test single element
100+
std::vector<int> single = {42};
101+
std::vector<int> expectedSingle = {42};
102+
assert(adaptiveMergeSort(single) == expectedSingle);
103+
104+
std::cout << "All AdaptiveMergeSort tests passed!" << std::endl;
105+
}
106+
107+
int main() {
108+
testAdaptiveMergeSort();
109+
return 0;
110+
}

0 commit comments

Comments
 (0)