diff --git a/bin/main.cpp b/bin/main.cpp index 8f408a4..58835dc 100644 --- a/bin/main.cpp +++ b/bin/main.cpp @@ -1,19 +1,27 @@ #include +#include "base/insertion_sort.hpp" + #include "numbers/gcd.hpp" #include "numbers/prime_factors.hpp" auto main() -> int { - hack::log()(algorithms::gcd(4851, 3003)); - hack::log()(algorithms::gcd(64, 28)); - hack::log()(algorithms::prime_factors_v1(127)); - hack::log()(algorithms::prime_factors_v1(128)); - hack::log()(algorithms::prime_factors_v1(130)); + // std::vector v { 2, 5, 4, 6, 1, 3 }; + std::vector v { 5, 4, 1, 5, 6 }; + algorithms::insertion_sort(v); + hack::log()(v); - hack::log()(algorithms::prime_factors_v2(127)); - hack::log()(algorithms::prime_factors_v2(128)); - hack::log()(algorithms::prime_factors_v2(130)); + // hack::log()(algorithms::gcd(4851, 3003)); + // hack::log()(algorithms::gcd(64, 28)); + // + // hack::log()(algorithms::prime_factors_v1(127)); + // hack::log()(algorithms::prime_factors_v1(128)); + // hack::log()(algorithms::prime_factors_v1(130)); + // + // hack::log()(algorithms::prime_factors_v2(127)); + // hack::log()(algorithms::prime_factors_v2(128)); + // hack::log()(algorithms::prime_factors_v2(130)); } diff --git a/src/base/insertion_sort.hpp b/src/base/insertion_sort.hpp new file mode 100644 index 0000000..f2c21fe --- /dev/null +++ b/src/base/insertion_sort.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include +#include + +// Сортировка вставкой +namespace algorithms +{ + inline void insertion_sort(std::vector& v) + { + for (std::size_t i = 1; i < v.size(); ++i) + { + auto key = v[i]; + auto j = i; + + while (j > 0 && v[j - 1] > key) + { + v[j] = v[j - 1]; + --j; + } + v[j] = key; + } + } +} + diff --git a/src/meson.build b/src/meson.build index 9622a56..6cb6931 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,6 +1,7 @@ inc += include_directories('.') headers = [ + 'base/insertion_sort.hpp', 'numbers/gcd.hpp', 'numbers/prime_factors.hpp' ]