Skip to content

Commit b71910d

Browse files
authored
Create stalin_sort.cpp
a new type of sorting
1 parent c2adc3b commit b71910d

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

sorting/stalin_sort.cpp

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/**
2+
* @file StalinSort.cpp
3+
* @brief Implementation of the StalinSort algorithm in C++.
4+
*
5+
* @details This file contains the implementation of StalinSort algorithm which
6+
* retains only non-decreasing elements in the list. Named after Stalin for its
7+
* ruthless removal of elements that don't meet the criteria.
8+
*
9+
* @see https://medium.com/@kaweendra/the-ultimate-sorting-algorithm-6513d6968420
10+
*/
11+
12+
#include <iostream>
13+
#include <vector>
14+
#include <string>
15+
#include <cassert>
16+
#include <algorithm>
17+
18+
/**
19+
* @brief Function to sort an array using StalinSort algorithm
20+
* @param array Vector of elements to be sorted
21+
* @return Vector of sorted elements
22+
*/
23+
template <typename T>
24+
std::vector<T> stalinSort(std::vector<T> array) {
25+
if (array.empty()) return array;
26+
27+
std::vector<T> result;
28+
result.push_back(array[0]);
29+
30+
for (size_t i = 1; i < array.size(); ++i) {
31+
if (array[i] >= result.back()) {
32+
result.push_back(array[i]);
33+
}
34+
}
35+
36+
return result;
37+
}
38+
39+
/**
40+
* @brief Function to sort an array using Adaptive Merge Sort algorithm
41+
* @param array Vector of elements to be sorted
42+
* @return Vector of sorted elements
43+
*/
44+
template <typename T>
45+
void merge(std::vector<T>& array, std::vector<T>& aux, int low, int mid, int high) {
46+
std::copy(array.begin() + low, array.begin() + high + 1, aux.begin() + low);
47+
48+
int i = low;
49+
int j = mid + 1;
50+
for (int k = low; k <= high; ++k) {
51+
if (i > mid) {
52+
array[k] = aux[j++];
53+
} else if (j > high) {
54+
array[k] = aux[i++];
55+
} else if (aux[j] < aux[i]) {
56+
array[k] = aux[j++];
57+
} else {
58+
array[k] = aux[i++];
59+
}
60+
}
61+
}
62+
63+
template <typename T>
64+
void sort(std::vector<T>& array, std::vector<T>& aux, int low, int high) {
65+
if (low >= high) return;
66+
67+
int mid = low + (high - low) / 2;
68+
sort(array, aux, low, mid);
69+
sort(array, aux, mid + 1, high);
70+
merge(array, aux, low, mid, high);
71+
}
72+
73+
template <typename T>
74+
std::vector<T> adaptiveMergeSort(std::vector<T> array) {
75+
if (array.size() <= 1) return array;
76+
77+
std::vector<T> aux(array.size());
78+
sort(array, aux, 0, array.size() - 1);
79+
return array;
80+
}
81+
82+
void testStalinSort() {
83+
// Test Integer Input
84+
std::vector<int> integers = {4, 23, 6, 78, 1, 54, 231, 9, 12};
85+
std::vector<int> expectedIntegers = {4, 23, 78, 231};
86+
assert(stalinSort(integers) == expectedIntegers);
87+
88+
// Test String Input
89+
std::vector<std::string> strings = {"c", "a", "e", "b", "d"};
90+
std::vector<std::string> expectedStrings = {"c", "e"};
91+
assert(stalinSort(strings) == expectedStrings);
92+
93+
// Test with duplicates
94+
std::vector<int> duplicates = {1, 3, 2, 2, 5, 4};
95+
std::vector<int> expectedDuplicates = {1, 3, 5};
96+
assert(stalinSort(duplicates) == expectedDuplicates);
97+
98+
// Test empty array
99+
std::vector<int> empty = {};
100+
std::vector<int> expectedEmpty = {};
101+
assert(stalinSort(empty) == expectedEmpty);
102+
103+
// Test single element
104+
std::vector<int> single = {42};
105+
std::vector<int> expectedSingle = {42};
106+
assert(stalinSort(single) == expectedSingle);
107+
108+
std::cout << "All StalinSort tests passed!" << std::endl;
109+
}
110+
111+
void testAdaptiveMergeSort() {
112+
// Test Integer Input
113+
std::vector<int> integers = {4, 23, 6, 78, 1, 54, 231, 9, 12};
114+
std::vector<int> expectedIntegers = {1, 4, 6, 9, 12, 23, 54, 78, 231};
115+
assert(adaptiveMergeSort(integers) == expectedIntegers);
116+
117+
// Test String Input
118+
std::vector<std::string> strings = {"c", "a", "e", "b", "d"};
119+
std::vector<std::string> expectedStrings = {"a", "b", "c", "d", "e"};
120+
assert(adaptiveMergeSort(strings) == expectedStrings);
121+
122+
// Test with duplicates
123+
std::vector<int> duplicates = {1, 3, 2, 2, 5, 4};
124+
std::vector<int> expectedDuplicates = {1, 2, 2, 3, 4, 5};
125+
assert(adaptiveMergeSort(duplicates) == expectedDuplicates);
126+
127+
// Test empty array
128+
std::vector<int> empty = {};
129+
std::vector<int> expectedEmpty = {};
130+
assert(adaptiveMergeSort(empty) == expectedEmpty);
131+
132+
// Test single element
133+
std::vector<int> single = {42};
134+
std::vector<int> expectedSingle = {42};
135+
assert(adaptiveMergeSort(single) == expectedSingle);
136+
137+
std::cout << "All AdaptiveMergeSort tests passed!" << std::endl;
138+
}
139+
140+
int main() {
141+
testStalinSort();
142+
testAdaptiveMergeSort();
143+
return 0;
144+
}

0 commit comments

Comments
 (0)