Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
stooge_sort.cpp File Reference

Stooge sort implementation in C++ More...

#include <vector>
#include <cassert>
#include <algorithm>
#include <iostream>
Include dependency graph for stooge_sort.cpp:

Functions

void stoogeSort (std::vector< int > *L, size_t i, size_t j)
 for IO operations
 
void test1 ()
 Function to test sorting algorithm.
 
void test2 ()
 Function to test sorting algorithm, one element.
 
void test3 ()
 Function to test sorting algorithm, repeating elements.
 
int main ()
 Main function.
 

Detailed Description

Stooge sort implementation in C++

Stooge sort is a recursive sorting algorithm. It divides the array into 3 parts and proceeds to:

  • sort first two thirds of the array
  • sort last two thirds of the array
  • sort first two thirds of the array It's time complexity is O(n^(log3/log1.5)), which is about O(n^2.7), which makes it to be not the most efficient sorting algorithm on the street on average. Space complexity is O(1).

Function Documentation

◆ main()

int main ( void )

Main function.

Parameters
argccommandline argument count (ignored)
argvcommandline array of arguments (ignored)
Returns
0 on exit
79 {
80 test1();
81 test2();
82 test3();
83
84 std::cout << "All tests have successfully passed!\n";
85 return 0;
86}
void test2()
Function to test sorting algorithm, one element.
Definition stooge_sort.cpp:57
void test1()
Function to test sorting algorithm.
Definition stooge_sort.cpp:47
void test3()
Function to test sorting algorithm, repeating elements.
Definition stooge_sort.cpp:67
Here is the call graph for this function:

◆ stoogeSort()

void stoogeSort ( std::vector< int > * L,
size_t i,
size_t j )

for IO operations

for vector for assert for std::is_sorted

The stoogeSort() function is used for sorting the array.

Parameters
L- vector of values (int) to be sorted in in place (ascending order)
i- the first index of the array (0)
j- the last index of the array (L.size() - 1)
Returns
void
28 {
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}
void stoogeSort(std::vector< int > *L, size_t i, size_t j)
for IO operations
Definition stooge_sort.cpp:28
T swap(T... args)
Here is the call graph for this function:

◆ test1()

void test1 ( )

Function to test sorting algorithm.

Returns
void
47 {
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}
T begin(T... args)
T end(T... args)
T is_sorted(T... args)
T size(T... args)
Here is the call graph for this function:

◆ test2()

void test2 ( )

Function to test sorting algorithm, one element.

Returns
void
57 {
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}
Here is the call graph for this function:

◆ test3()

void test3 ( )

Function to test sorting algorithm, repeating elements.

Returns
void
67 {
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}
Here is the call graph for this function: