Files
sorting_algorithms/data.cpp

82 lines
3.6 KiB
C++

#include <random>
#include "toolbox.hpp"
#include "data.hpp"
template<typename T>
static constexpr bool is_arg_sorted(const asp::Array<T>& a, const asp::Array<size_t>& indices) noexcept {
for (size_t i = 1; i < a.length; ++i)
if (indices[i - 1] == indices[i] || a[indices[i - 1]] > a[indices[i]])
return false;
return true;
}
template<typename T>
static constexpr bool is_sorted(const asp::Array<T>& a) noexcept {
for (size_t i = 1; i < a.length; ++i)
if (a[i - 1] > a[i])
return false;
return true;
}
template<typename T, size_t N>
static constexpr void test_sort(const std::array<int32_t, N>& gaps, const asp::Array<T>& original, void (* const fnc)(asp::Array<T>&), const char* const title) noexcept {
asp::Array<T> a(original);
asp::measure_time_void(gaps, title, fnc, a);
const bool result = is_sorted<T>(a);
asp::formatted_row(gaps, { result ? "=> Unit test success" : "=> Unit test failure", "-", "-" });
}
template<typename T, size_t N>
static inline void test_sort(const std::array<int32_t, N>& gaps, const asp::Array<T>& original, asp::Array<size_t>(* const fnc)(asp::Array<T>&), const char* const title) noexcept {
asp::Array<T> a(original);
const asp::Array<size_t> indices = asp::measure_time<asp::Array<size_t>>(gaps, title, fnc, a);
const bool result = is_arg_sorted<T>(original, indices);
asp::formatted_row(gaps, { result ? "=> Unit test success" : "=> Unit test failure", "-", "-" });
}
template<typename T>
static inline asp::Array<T> create_random_array(const int64_t n) noexcept {
asp::Array<T> original((size_t(n)));
std::random_device rd;
std::default_random_engine gen(rd());
std::uniform_int_distribution<T> distribution(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
return std::move(asp::map(original, [&distribution, &gen](const size_t&, const T&) -> const T { return distribution(gen); }));
}
int32_t main(int32_t argc, char** argv) {
asp::toolbox_unit_test();
using array_type = uint16_t;
int64_t N = (static_cast<int64_t>(1)<<16) - 1;
// int64_t N = std::numeric_limits<array_type>::max();
if (argc > 2) {
fprintf(stderr, "Too many arguments\nUsage: ./data <ARRAY_SIZE>\n");
return EXIT_FAILURE;
} else if (argc == 2)
N = std::strtol(argv[1], argv + strlen(argv[1]) + 1, 10);
char title[64];
sprintf(title, "Sorting %s elements of %s", asp::thousand_sep(N).c_str(), asp::format_byte_size(2 * uint64_t(N) * sizeof(array_type)).c_str());
constexpr std::array<int32_t, 3> gaps = { 48, -17, 25 };
asp::header(gaps, { title, "Time spent (ns)", "Formatted time spent" });
const asp::Array<array_type> original = asp::measure_time<asp::Array<array_type>>(gaps, "Creating random array", create_random_array<array_type>, N);
test_sort(gaps, original, asp::bubble_sort<array_type>, "Bubble sort");
test_sort(gaps, original, asp::bubble_sort_arg<array_type>, "Bubble sort (arg)");
test_sort(gaps, original, asp::quicksort<array_type>, "Quicksort");
test_sort(gaps, original, asp::quicksort_arg<array_type>, "Quicksort (arg)");
test_sort(gaps, original, asp::quicksort_iter<array_type>, "Quicksort (iterative)");
test_sort(gaps, original, asp::quicksort_arg_iter<array_type>, "Quicksort (iterative) (arg)");
test_sort(gaps, original, asp::mergesort<array_type>, "Mergesort");
test_sort(gaps, original, asp::mergesort_arg<array_type>, "Mergesort (arg)");
test_sort(gaps, original, asp::insertion_sort<array_type>, "Insertion sort");
test_sort(gaps, original, asp::insertion_argsort<array_type>, "Insertion (arg)");
test_sort(gaps, original, asp::counting_sort<array_type>, "Counting sort");
test_sort(gaps, original, asp::radix_sort<array_type>, "Radix sort");
asp::footer(gaps);
return EXIT_SUCCESS;
}