diff --git a/README.md b/README.md index 6a8a5efd..e4135e99 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ handle it from there. :smile: * [Insertion sort](cpp/include/algorithm/sorting/insertion_sort.hpp) :white_check_mark: * [Merge sort](cpp/include/algorithm/sorting/merge_sort.hpp) :white_check_mark: * [Quick sort](cpp/include/algorithm/sorting/quick_sort.hpp) :white_check_mark: - * [Radix sort](cpp/include/algorithm/sorting/radix_sort.hpp) + * [Radix sort](cpp/include/algorithm/sorting/radix_sort.hpp) :white_check_mark: * [Selection sort](cpp/include/algorithm/sorting/selection_sort.hpp) :white_check_mark: * [Shell sort](cpp/include/algorithm/sorting/shell_sort.hpp) :white_check_mark: diff --git a/cpp/include/algorithm/sorting/radix_sort.hpp b/cpp/include/algorithm/sorting/radix_sort.hpp index 0b8cefe6..adef94dc 100644 --- a/cpp/include/algorithm/sorting/radix_sort.hpp +++ b/cpp/include/algorithm/sorting/radix_sort.hpp @@ -85,12 +85,12 @@ int max_in_vector(const vector& values) { */ void radix_sort_internal(vector& values, const int mult_factor, const int add_factor, const bool to_show_state = false) { - int max_value = max_in_vect(values); + int max_value = max_in_vector(values); // On each iteration of the following loop, extractor helps in getting the // next significant digit, which is (value / extractor) mod 10 for (int extractor = 1; max_value / extractor > 0; extractor *= 10) { - count_sort(values, extractor, to_show_state, mult_factor, add_factor); + count_sort(values, extractor, mult_factor, add_factor, to_show_state); } } diff --git a/cpp/test/algorithm/sorting/sorting.cpp b/cpp/test/algorithm/sorting/sorting.cpp index 56390159..d289ac63 100644 --- a/cpp/test/algorithm/sorting/sorting.cpp +++ b/cpp/test/algorithm/sorting/sorting.cpp @@ -9,12 +9,13 @@ #include "algorithm/sorting/insertion_sort.hpp" #include "algorithm/sorting/merge_sort.hpp" #include "algorithm/sorting/quick_sort.hpp" +#include "algorithm/sorting/radix_sort.hpp" #include "algorithm/sorting/selection_sort.hpp" #include "algorithm/sorting/shell_sort.hpp" // Prototypes int generate_random_int(int, int); -vector generate_unsorted_vector(int max_size = 1000); +vector generate_unsorted_vector(const bool is_unsigned = false, int max_size = 1000); // Pointer to function using sorting_function = void(*)(vector&, int, bool); @@ -50,6 +51,14 @@ TEST_CASE("Sort in ascending order", "[sorting]") { REQUIRE(algo_sorted == std_sorted); algo_sorted = original; } + + // radix sort special case, since curr impl supports only unsigned + original = algo_sorted = std_sorted = generate_unsorted_vector(true); + std::sort(std_sorted.begin(), std_sorted.end()); + + // Run tests + radix_sort(algo_sorted, 1, false); + REQUIRE(algo_sorted == std_sorted); } } @@ -81,6 +90,14 @@ TEST_CASE("Sort in descending order", "[sorting]") { REQUIRE(algo_sorted == std_sorted); algo_sorted = original; } + + // radix sort special case, since curr impl supports only unsigned + original = algo_sorted = std_sorted = generate_unsorted_vector(true); + std::sort(std_sorted.rbegin(), std_sorted.rend()); + + // Run tests + radix_sort(algo_sorted, -1, false); + REQUIRE(algo_sorted == std_sorted); } } @@ -90,13 +107,26 @@ TEST_CASE("Sort in descending order", "[sorting]") { Creates a vector of random size and populates it with random integers. Default for max_size is set in function declaration. */ -vector generate_unsorted_vector(int max_size) { +vector generate_unsorted_vector(const bool is_unsigned, int max_size) { vector v; auto vector_size = (size_t) generate_random_int(1, max_size); v.reserve(vector_size); + int min, max; + + if (is_unsigned) + { + min = std::numeric_limits::min(); + max = std::numeric_limits::max(); + } + else + { + min = std::numeric_limits::min(); + max = std::numeric_limits::max(); + } + for (int i = 0; i < (int) vector_size; i++) { - v.push_back(generate_random_int(std::numeric_limits::min(), std::numeric_limits::max())); + v.push_back(generate_random_int(min, max)); } return v; }