Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

radix fixes and unit test #420

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
4 changes: 2 additions & 2 deletions cpp/include/algorithm/sorting/radix_sort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ int max_in_vector(const vector<int>& values) {
*/
void radix_sort_internal(vector<int>& 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);
}
}

Expand Down
36 changes: 33 additions & 3 deletions cpp/test/algorithm/sorting/sorting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> generate_unsorted_vector(int max_size = 1000);
vector<int> generate_unsorted_vector(const bool is_unsigned = false, int max_size = 1000);

// Pointer to function
using sorting_function = void(*)(vector<int>&, int, bool);
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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<int> generate_unsorted_vector(int max_size) {
vector<int> generate_unsorted_vector(const bool is_unsigned, int max_size) {
vector<int> 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<unsigned short>::min();
max = std::numeric_limits<unsigned short>::max();
}
else
{
min = std::numeric_limits<short>::min();
max = std::numeric_limits<short>::max();
}

for (int i = 0; i < (int) vector_size; i++) {
v.push_back(generate_random_int(std::numeric_limits<short>::min(), std::numeric_limits<short>::max()));
v.push_back(generate_random_int(min, max));
}
return v;
}
Expand Down
Loading