TheAlgorithms/C++ 1.0.0
All the 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:

Go to the source code of this file.

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).

Definition in file stooge_sort.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Parameters
argccommandline argument count (ignored)
argvcommandline array of arguments (ignored)
Returns
0 on exit

Definition at line 79 of file stooge_sort.cpp.

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.
void test1()
Function to test sorting algorithm.
void test3()
Function to test sorting algorithm, repeating elements.

◆ 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

Definition at line 28 of file stooge_sort.cpp.

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

◆ test1()

void test1 ( )

Function to test sorting algorithm.

Returns
void

Definition at line 47 of file stooge_sort.cpp.

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}

◆ test2()

void test2 ( )

Function to test sorting algorithm, one element.

Returns
void

Definition at line 57 of file stooge_sort.cpp.

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}

◆ test3()

void test3 ( )

Function to test sorting algorithm, repeating elements.

Returns
void

Definition at line 67 of file stooge_sort.cpp.

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}