103 lines
2.9 KiB
C++
Raw Normal View History

2020-06-12 11:54:50 +04:00
/**
* @file
2020-07-17 20:00:36 -04:00
* @brief Implementation of [gnome
* sort](https://en.wikipedia.org/wiki/Gnome_sort) algorithm.
* @author [beqakd](https://github.com/beqakd)
* @author [Krishna Vedala](https://github.com/kvedala)
* @details
* Gnome sort algorithm is not the best one but it is widely used.
* The algorithm iteratively checks the order of pairs in the array. If they are
* on right order it moves to the next successive pair, otherwise it swaps
* elements. This operation is repeated until no more swaps are made thus
* indicating the values to be in ascending order.
*
* The time Complexity of the algorithm is \f$O(n^2)\f$ and in some cases it
* can be \f$O(n)\f$.
2020-06-12 11:54:50 +04:00
*/
2020-06-05 18:09:55 +04:00
2020-07-17 20:00:36 -04:00
#include <algorithm> // for std::swap
#include <array> // for std::array
#include <iostream> // for io operations
2020-06-12 11:54:50 +04:00
/**
2020-07-17 20:00:36 -04:00
* @namespace sorting
* Sorting algorithms
*/
namespace sorting {
/**
* This implementation is for a C-style array input that gets modified in place.
* @param [in,out] arr our array of elements.
2020-06-12 11:54:50 +04:00
* @param size size of given array
*/
2020-07-17 20:00:36 -04:00
template <typename T>
void gnomeSort(T *arr, int size) {
2020-06-08 10:22:45 +04:00
// few easy cases
2020-07-17 20:00:36 -04:00
if (size <= 1) return;
2020-06-05 18:09:55 +04:00
2020-06-12 12:46:31 +04:00
int index = 0; // initialize some variables.
2020-06-08 10:22:45 +04:00
while (index < size) {
// check for swap
2020-06-12 12:45:07 +04:00
if ((index == 0) || (arr[index] >= arr[index - 1])) {
2020-07-17 20:00:36 -04:00
index++;
2020-06-08 10:22:45 +04:00
} else {
2020-07-17 20:00:36 -04:00
std::swap(arr[index], arr[index - 1]); // swap
index--;
2020-06-08 10:22:45 +04:00
}
2020-06-05 18:09:55 +04:00
}
}
2020-06-08 10:22:45 +04:00
2020-06-12 11:54:50 +04:00
/**
2020-07-17 20:00:36 -04:00
* This implementation is for a C++-style array input. The function is a
* pass-by-value and hence a copy of the array gets created which is then
* modified by the function and returned.
* @tparam T type of data variables in the array
* @tparam size size of the array
* @param [in] arr our array of elements.
* @return array with elements sorted
2020-06-12 11:54:50 +04:00
*/
2020-07-17 20:00:36 -04:00
template <typename T, size_t size>
std::array<T, size> gnomeSort(std::array<T, size> arr) {
// few easy cases
if (size <= 1) return;
int index = 1; // initialize loop index
while (index < size) {
// check for swap
if (arr[index] >= arr[index - 1]) {
index++;
} else {
std::swap(arr[index], arr[index - 1]); // swap
index--;
}
}
return arr;
}
} // namespace sorting
/**
* Test function
*/
static void test() {
2020-06-12 12:43:05 +04:00
// Example 1. Creating array of int,
2020-07-17 20:00:36 -04:00
std::cout << "Test 1 - as a C-array...";
int size = 6;
std::array<int, size> arr = {-22, 100, 150, 35, -10, 99};
sorting::gnomeSort(arr.data, size);
// for (int i = 0; i < size; i++) std::cout << arr[i] << " ";
2020-06-12 12:45:07 +04:00
std::cout << "\n" << std::endl;
2020-06-12 12:43:05 +04:00
// Example 2. Creating array of doubles.
2020-07-17 20:00:36 -04:00
std::array<double, size> double_arr = {-100.2, 10.2, 20.0, 9.0, 7.5, 7.2};
std::array<double, size> sorted_arr = sorting::gnomeSort(double_arr);
for (int i = 0; i < size; i++) std::cout << double_arr[i] << " ";
}
/**
* Our main function with example of sort method.
*/
int main() {
test();
2020-06-08 10:22:45 +04:00
return 0;
2020-06-05 18:09:55 +04:00
}