add namespace - sorting

This commit is contained in:
Krishna Vedala 2020-05-31 23:00:59 -04:00
parent 1125c85b3f
commit c2c4681554

View File

@ -1,11 +1,17 @@
/**
* \file
* \brief [Shell sort](https://en.wikipedia.org/wiki/Shell_sort) algorithm
*/
#include <array> #include <array>
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
#include <iostream> #include <iostream>
#include <utility> // for std::swap
// for std::swap /** pretty print array
#include <utility> * \param[in] arr array to print
* \param[in] LEN length of array to print
*/
template <class T> template <class T>
void show_data(T *arr, size_t LEN) { void show_data(T *arr, size_t LEN) {
size_t i; size_t i;
@ -14,17 +20,25 @@ void show_data(T *arr, size_t LEN) {
std::cout << std::endl; std::cout << std::endl;
} }
/** pretty print array
* \param[in] arr array to print
* \param[in] N length of array to print
*/
template <class T, size_t N> template <class T, size_t N>
void show_data(T (&arr)[N]) { void show_data(T (&arr)[N]) {
show_data(arr, N); show_data(arr, N);
} }
/** /** \namespace sorting
* \brief Sorting algorithms
*/
namespace sorting {
/**
* Optimized algorithm - takes half the time by utilizing * Optimized algorithm - takes half the time by utilizing
* Mar * Mar
**/ **/
template <class T> template <class T>
void shell_sort(T *arr, size_t LEN) { void shell_sort(T *arr, size_t LEN) {
const unsigned int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1}; const unsigned int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
const unsigned int gap_len = 8; const unsigned int gap_len = 8;
size_t i, j, g; size_t i, j, g;
@ -40,12 +54,18 @@ void shell_sort(T *arr, size_t LEN) {
arr[j] = tmp; arr[j] = tmp;
} }
} }
} }
template <class T, size_t N> /** function overload - when input array is of a known length array type
void shell_sort(T (&arr)[N]) { */
template <class T, size_t N>
void shell_sort(T (&arr)[N]) {
shell_sort(arr, N); shell_sort(arr, N);
} }
} // namespace sorting
using sorting::shell_sort;
/** /**
* function to compare sorting using cstdlib's qsort * function to compare sorting using cstdlib's qsort