Repository: TheAlgorithms/C-Plus-Plus
File: sorting/stooge_sort.cpp (C++)

Code before:

#include <vector> /// for vector
#include <cassert> /// for assert
#include <algorithm>  /// for std::is_sorted
#include <iostream>   /// for IO operations

 /**
  * @brief The stoogeSort() function is used for sorting the array.
  * @param L - vector of values (int) to be sorted in in place (ascending order)
  * @param i - the first index of the array (0)
  * @param j - the last index of the array (L.size() - 1)
  * @returns void
  */
void stoogeSort(std::vector<int>* L, size_t i, size_t j) {
	if (i < j) {
		if ((*L)[i] > (*L)[j]) {
			std::swap((*L)[i], (*L)[j]);
		}
		if (j - i > 1) {
			size_t third = (j - i + 1) / 3;
			stoogeSort(L, i, j - third);
			stoogeSort(L, i + third, j);
			stoogeSort(L, i, j - third);
		}
	}
	return;
}

/**
 * @brief Function to test sorting algorithm
 * @returns void
 */
void test1() {
	std::vector<int> L = { 8, 9, 10, 4, 3, 5, 1 };
	stoogeSort(&L, 0, L.size() - 1);
	assert(std::is_sorted(std::begin(L), std::end(L)));
}

/**
 * @brief Function to test sorting algorithm, one element
 * @returns void
 */
void test2() {
	std::vector<int> L = { -1 };
	stoogeSort(&L, 0, L.size() - 1);
	assert(std::is_sorted(std::begin(L), std::end(L)));
}

/**
 * @brief Function to test sorting algorithm, repeating elements
 * @returns void

Review comment (suggestion):
This is logically correct but could you think of a different way to write this? `@alexpantyukhin` gave the correct suggestion but you've marked it as resolved without considering its impact.

```cpp
void stoogeSort(std::vector<int>* L, size_t i, size_t j) {
 	if (i >= j) {
		return;
	}
	if ((*L)[i] > (*L)[j]) {
 		std::swap((*L)[i], (*L)[j]);
 	}
 	if (j - i > 1) {
 		size_t third = (j - i + 1) / 3;
 		stoogeSort(L, i, j - third);
 		stoogeSort(L, i + third, j);
 		stoogeSort(L, i, j - third);
 	}
}
```

Just adding the guard clause

```cpp
 	if (i >= j) {
		return;
	}
```

makes so much difference in nesting. The use of a guard clause makes reading your code easier for people reviewing your code, and remember that code is read more often than written therefore you should write it in a way to make this easier for readers.


Code after:

#include <vector> /// for vector
#include <cassert> /// for assert
#include <algorithm>  /// for std::is_sorted
#include <iostream>   /// for IO operations

 /**
  * @brief The stoogeSort() function is used for sorting the array.
  * @param L - vector of values (int) to be sorted in in place (ascending order)
  * @param i - the first index of the array (0)
  * @param j - the last index of the array (L.size() - 1)
  * @returns void
  */
void stoogeSort(std::vector<int>* L, size_t i, size_t j) {
    if (i >= j) {
        return;
    }
    if ((*L)[i] > (*L)[j]) {
        std::swap((*L)[i], (*L)[j]);
    }
    if (j - i > 1) {
        size_t third = (j - i + 1) / 3;
        stoogeSort(L, i, j - third);
        stoogeSort(L, i + third, j);
        stoogeSort(L, i, j - third);
    }
}

/**
 * @brief Function to test sorting algorithm
 * @returns void
 */
void test1() {
	std::vector<int> L = { 8, 9, 10, 4, 3, 5, 1 };
	stoogeSort(&L, 0, L.size() - 1);
	assert(std::is_sorted(std::begin(L), std::end(L)));
}

/**
 * @brief Function to test sorting algorithm, one element
 * @returns void
 */
void test2() {
	std::vector<int> L = { -1 };
	stoogeSort(&L, 0, L.size() - 1);
	assert(std::is_sorted(std::begin(L), std::end(L)));
}

/**
 * @brief Function to test sorting algorithm, repeating elements
 * @returns void