Skip to content

Commit 29f5439

Browse files
feat: add Stooge sort algorithm (#2440)
* feat: add Stooge sort algorithm * docs: add documentation for function parameters and includes * test: add new tests for the Stooge sort * docs: Update functions descriptions * fix: Update parameter type to size_t * doc: Add message that the tests have passed * fix: Update return statement structure --------- Co-authored-by: David Leal <[email protected]>
1 parent f04e038 commit 29f5439

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

sorting/stooge_sort.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* @file
3+
* @brief [Stooge sort implementation](https://en.wikipedia.org/wiki/Stooge_sort)
4+
* in C++
5+
* @details
6+
* Stooge sort is a recursive sorting algorithm.
7+
* It divides the array into 3 parts and proceeds to:
8+
* - sort first two thirds of the array
9+
* - sort last two thirds of the array
10+
* - sort first two thirds of the array
11+
* It's time complexity is O(n^(log3/log1.5)), which is about O(n^2.7),
12+
* which makes it to be not the most efficient sorting algorithm
13+
* on the street on average. Space complexity is O(1).
14+
*/
15+
16+
#include <vector> /// for vector
17+
#include <cassert> /// for assert
18+
#include <algorithm> /// for std::is_sorted
19+
#include <iostream> /// for IO operations
20+
21+
/**
22+
* @brief The stoogeSort() function is used for sorting the array.
23+
* @param L - vector of values (int) to be sorted in in place (ascending order)
24+
* @param i - the first index of the array (0)
25+
* @param j - the last index of the array (L.size() - 1)
26+
* @returns void
27+
*/
28+
void stoogeSort(std::vector<int>* L, size_t i, size_t j) {
29+
if (i >= j) {
30+
return;
31+
}
32+
if ((*L)[i] > (*L)[j]) {
33+
std::swap((*L)[i], (*L)[j]);
34+
}
35+
if (j - i > 1) {
36+
size_t third = (j - i + 1) / 3;
37+
stoogeSort(L, i, j - third);
38+
stoogeSort(L, i + third, j);
39+
stoogeSort(L, i, j - third);
40+
}
41+
}
42+
43+
/**
44+
* @brief Function to test sorting algorithm
45+
* @returns void
46+
*/
47+
void test1() {
48+
std::vector<int> L = { 8, 9, 10, 4, 3, 5, 1 };
49+
stoogeSort(&L, 0, L.size() - 1);
50+
assert(std::is_sorted(std::begin(L), std::end(L)));
51+
}
52+
53+
/**
54+
* @brief Function to test sorting algorithm, one element
55+
* @returns void
56+
*/
57+
void test2() {
58+
std::vector<int> L = { -1 };
59+
stoogeSort(&L, 0, L.size() - 1);
60+
assert(std::is_sorted(std::begin(L), std::end(L)));
61+
}
62+
63+
/**
64+
* @brief Function to test sorting algorithm, repeating elements
65+
* @returns void
66+
*/
67+
void test3() {
68+
std::vector<int> L = { 1, 2, 5, 4, 1, 5 };
69+
stoogeSort(&L, 0, L.size() - 1);
70+
assert(std::is_sorted(std::begin(L), std::end(L)));
71+
}
72+
73+
/**
74+
* @brief Main function
75+
* @param argc commandline argument count (ignored)
76+
* @param argv commandline array of arguments (ignored)
77+
* @returns 0 on exit
78+
*/
79+
int main() {
80+
test1();
81+
test2();
82+
test3();
83+
84+
std::cout << "All tests have successfully passed!\n";
85+
return 0;
86+
}

0 commit comments

Comments
 (0)