From ad7740c6610356a0859e2c17f7826d5c7fc22ed3 Mon Sep 17 00:00:00 2001 From: NikaKrasnopevtseva Date: Mon, 23 Feb 2026 09:46:00 +0000 Subject: [PATCH 1/9] add my task --- .../common/include/common.hpp | 16 +++ .../data/pic.ppm | Bin 0 -> 23 bytes .../info.json | 9 ++ .../report.md | 0 .../seq/include/ops_seq.hpp | 25 ++++ .../seq/report.md | 0 .../seq/src/ops_seq.cpp | 119 ++++++++++++++++++ .../settings.json | 10 ++ .../tests/functional/main.cpp | 90 +++++++++++++ .../tests/performance/main.cpp | 62 +++++++++ 10 files changed, 331 insertions(+) create mode 100644 tasks/krasnopevtseva_v_hoare_batcher_sort/common/include/common.hpp create mode 100644 tasks/krasnopevtseva_v_hoare_batcher_sort/data/pic.ppm create mode 100644 tasks/krasnopevtseva_v_hoare_batcher_sort/info.json create mode 100644 tasks/krasnopevtseva_v_hoare_batcher_sort/report.md create mode 100644 tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp create mode 100644 tasks/krasnopevtseva_v_hoare_batcher_sort/seq/report.md create mode 100644 tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp create mode 100644 tasks/krasnopevtseva_v_hoare_batcher_sort/settings.json create mode 100644 tasks/krasnopevtseva_v_hoare_batcher_sort/tests/functional/main.cpp create mode 100644 tasks/krasnopevtseva_v_hoare_batcher_sort/tests/performance/main.cpp diff --git a/tasks/krasnopevtseva_v_hoare_batcher_sort/common/include/common.hpp b/tasks/krasnopevtseva_v_hoare_batcher_sort/common/include/common.hpp new file mode 100644 index 00000000..e6ce6b86 --- /dev/null +++ b/tasks/krasnopevtseva_v_hoare_batcher_sort/common/include/common.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include +#include +#include + +#include "task/include/task.hpp" + +namespace krasnopevtseva_v_hoare_batcher_sort { + +using InType = std::vector; +using OutType = std::vector; +using TestType = std::tuple, std::string>; +using BaseTask = ppc::task::Task; + +} // namespace krasnopevtseva_v_hoare_batcher_sort diff --git a/tasks/krasnopevtseva_v_hoare_batcher_sort/data/pic.ppm b/tasks/krasnopevtseva_v_hoare_batcher_sort/data/pic.ppm new file mode 100644 index 0000000000000000000000000000000000000000..637624238c89d914613ed301968bffbf462bc110 GIT binary patch literal 23 bcmWGA<1$h(;xaNd<@(RSzyQYo|NjR7KDY &arr, int left, int right); + void quickBatcherSort(std::vector &arr, int left, int right); + bool PostProcessingImpl() override; +}; + +} // namespace krasnopevtseva_v_hoare_batcher_sort diff --git a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/report.md b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/report.md new file mode 100644 index 00000000..e69de29b diff --git a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp new file mode 100644 index 00000000..c61fdf0b --- /dev/null +++ b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp @@ -0,0 +1,119 @@ +#include "krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp" + +#include +#include + +#include "krasnopevtseva_v_hoare_batcher_sort/common/include/common.hpp" +#include "util/include/util.hpp" + +namespace krasnopevtseva_v_hoare_batcher_sort { + +KrasnopevtsevaVHoareBatcherSortSEQ::KrasnopevtsevaVHoareBatcherSortSEQ(const InType &in) { + SetTypeOfTask(GetStaticTypeOfTask()); + GetInput() = in; + GetOutput() = std::vector(); +} + +bool KrasnopevtsevaVHoareBatcherSortSEQ::ValidationImpl() { + const auto &input = GetInput(); + return (!input.empty()); +} + +bool KrasnopevtsevaVHoareBatcherSortSEQ::PreProcessingImpl() { + GetOutput() = std::vector(); + return true; +} + +bool KrasnopevtsevaVHoareBatcherSortSEQ::RunImpl() { + const auto &input = GetInput(); + size_t size = input.size(); + std::vector sort_v = input; + + if (size > 1) { + quickBatcherSort(sort_v, 0, size - 1); + } + GetOutput() = sort_v; + return true; +} + +void KrasnopevtsevaVHoareBatcherSortSEQ::compareAndSwap(int &a, int &b) { + if (a > b) { + std::swap(a, b); + } +} + +void KrasnopevtsevaVHoareBatcherSortSEQ::batcherMerge(std::vector &arr, int left, int right) { + int n = right - left + 1; + if (n <= 1) { + return; + } + + std::vector temp(arr.begin() + left, arr.begin() + right + 1); + + std::function oddEvenMerge = [&](int l, int r) { + if (l == r) { + return; + } + + int m = l + (r - l) / 2; + oddEvenMerge(l, m); + oddEvenMerge(m + 1, r); + + for (int i = l + 1; i + (m - l + 1) <= r; i += 2) { + compareAndSwap(temp[i], temp[i + (m - l + 1)]); + } + }; + + oddEvenMerge(0, n - 1); + + for (int i = 1; i + 1 < n; i += 2) { + compareAndSwap(temp[i], temp[i + 1]); + } + for (int i = 0; i < n; i++) { + arr[left + i] = temp[i]; + } +} + +void KrasnopevtsevaVHoareBatcherSortSEQ::quickBatcherSort(std::vector &arr, int left, int right) { + if (left >= right) { + return; + } + + int mid = left + (right - left) / 2; + if (arr[left] > arr[mid]) { + std::swap(arr[left], arr[mid]); + } + if (arr[left] > arr[right]) { + std::swap(arr[left], arr[right]); + } + if (arr[mid] > arr[right]) { + std::swap(arr[mid], arr[right]); + } + + std::swap(arr[mid], arr[right]); + int pivot = arr[right]; + + int i = left - 1; + int j = right; + + while (true) { + while (arr[++i] < pivot); + while (arr[--j] > pivot); + if (i >= j) { + break; + } + std::swap(arr[i], arr[j]); + } + std::swap(arr[i], arr[right]); + + quickBatcherSort(arr, left, i - 1); + quickBatcherSort(arr, i + 1, right); + + if (right - left > 32) { + batcherMerge(arr, left, right); + } +} +bool KrasnopevtsevaVHoareBatcherSortSEQ::PostProcessingImpl() { + return true; +} +} // namespace krasnopevtseva_v_hoare_batcher_sort diff --git a/tasks/krasnopevtseva_v_hoare_batcher_sort/settings.json b/tasks/krasnopevtseva_v_hoare_batcher_sort/settings.json new file mode 100644 index 00000000..0be0208f --- /dev/null +++ b/tasks/krasnopevtseva_v_hoare_batcher_sort/settings.json @@ -0,0 +1,10 @@ +{ + "tasks": { + "all": "enabled", + "omp": "enabled", + "seq": "enabled", + "stl": "enabled", + "tbb": "enabled" + }, + "tasks_type": "threads" +} diff --git a/tasks/krasnopevtseva_v_hoare_batcher_sort/tests/functional/main.cpp b/tasks/krasnopevtseva_v_hoare_batcher_sort/tests/functional/main.cpp new file mode 100644 index 00000000..6fb34c9c --- /dev/null +++ b/tasks/krasnopevtseva_v_hoare_batcher_sort/tests/functional/main.cpp @@ -0,0 +1,90 @@ +#include + +#include +#include +#include +#include +#include +#include + +#include "krasnopevtseva_v_hoare_batcher_sort/common/include/common.hpp" +#include "krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp" +#include "util/include/func_test_util.hpp" +#include "util/include/util.hpp" + +namespace krasnopevtseva_v_hoare_batcher_sort { + +class KrasnopevtsevaVRunFuncTestsThreads : public ppc::util::BaseRunFuncTests { + public: + static std::string PrintTestParam(const TestType &test_param) { + const auto &input = std::get<0>(test_param); + size_t size = input.size(); + std::string s; + for (size_t i = 0; i < size; i++) { + s += std::to_string(input[i]); + if (i < size - 1) { + s += "_"; + } + } + std::string result = "array_" + s + "_size" + std::to_string(size) + "_" + std::get<1>(test_param); + return result; + } + + protected: + void SetUp() override { + auto test_param = std::get(ppc::util::GTestParamIndex::kTestParams)>(GetParam()); + input_data_ = std::get<0>(test_param); + } + + bool CheckTestOutputData(OutType &output_data) final { + size_t size = output_data.size(); + bool result = true; + for (size_t i = 0; i < size - 1; i++) { + if (output_data[i] > output_data[i + 1]) { + result = false; + } + } + return result; + } + + InType GetTestInputData() final { + return input_data_; + } + + private: + InType input_data_; +}; + +namespace { + +TEST_P(KrasnopevtsevaVRunFuncTestsThreads, HoareBatcherSort) { + ExecuteTest(GetParam()); +} + +const std::array kTestParam = { + std::make_tuple(std::vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, "sorted_array"), + std::make_tuple(std::vector{5, 1, 3, 4, 2, 34, 24, 16, 31, 666, 22, 14, 52, 67, 13, 99, 9, 6, 28, 35}, + "default_array"), + std::make_tuple(std::vector{10, 1, 30, 2}, "short_array"), + std::make_tuple(std::vector{1000, 3246, 10, 31, 120, 4, 2, 1000, 23, 34, 30, 42, 1, + 45, 24, 15, 32, 111, 35, 25, 252, 222, 66234, 2325, 23423, 2355, + 745, 579, 875, 33, 66, 345, 4666, 2490, 100, 10, 3415, 234, 22, + 526, 372, 8432, 21, 58, 225, 865, 23, 13333, 35, 2523, 33}, + "long_array"), + std::make_tuple(std::vector{10, 20, 20, 10, 20, 10, 20, 10, 20}, "two_number_array"), + std::make_tuple(std::vector{10000, 9999, 9998, 4356, 3662, 3000, 2500, 2300, 2200, 2000, 1999, 1900, + 1843, 1000, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, + "end_to_begin_array")}; + +const auto kTestTasksList = ppc::util::AddFuncTask( + kTestParam, PPC_SETTINGS_krasnopevtseva_v_hoare_batcher_sort); + +const auto kGtestValues = ppc::util::ExpandToValues(kTestTasksList); + +const auto kPerfTestName = KrasnopevtsevaVRunFuncTestsThreads::PrintFuncTestName; + +INSTANTIATE_TEST_SUITE_P(HoareBatcherSort, KrasnopevtsevaVRunFuncTestsThreads, kGtestValues, kPerfTestName); + +} // namespace + +} // namespace krasnopevtseva_v_hoare_batcher_sort diff --git a/tasks/krasnopevtseva_v_hoare_batcher_sort/tests/performance/main.cpp b/tasks/krasnopevtseva_v_hoare_batcher_sort/tests/performance/main.cpp new file mode 100644 index 00000000..67480722 --- /dev/null +++ b/tasks/krasnopevtseva_v_hoare_batcher_sort/tests/performance/main.cpp @@ -0,0 +1,62 @@ +#include + +#include +#include +#include +#include + +#include "krasnopevtseva_v_hoare_batcher_sort/common/include/common.hpp" +#include "krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp" +#include "util/include/perf_test_util.hpp" + +namespace krasnopevtseva_v_hoare_batcher_sort { + +class KrasnopevtsevaVRunPerfTestsThreads : public ppc::util::BaseRunPerfTests { + InType input_data_; + bool res_{}; + + void SetUp() override { + std::vector vec(100000); + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution dist(0, 100000000); + + for (size_t i = 0; i < 100000; ++i) { + vec[i] = dist(gen); + } + input_data_ = vec; + } + + bool CheckTestOutputData(OutType &output_data) final { + size_t size = output_data.size(); + res_ = true; + for (size_t i = 0; i < size - 1; i++) { + if (output_data[i] > output_data[i + 1]) { + res_ = false; + } + } + return res_; + } + InType GetTestInputData() final { + return input_data_; + } +}; + +TEST_P(KrasnopevtsevaVRunPerfTestsThreads, RunPerfModes) { + ExecuteTest(GetParam()); +} + +namespace { + +const auto kAllPerfTasks = ppc::util::MakeAllPerfTasks( + PPC_SETTINGS_krasnopevtseva_v_hoare_batcher_sort); + +const auto kGtestValues = ppc::util::TupleToGTestValues(kAllPerfTasks); + +const auto kPerfTestName = KrasnopevtsevaVRunPerfTestsThreads::CustomPerfTestName; + +INSTANTIATE_TEST_SUITE_P(RunModeTests, KrasnopevtsevaVRunPerfTestsThreads, kGtestValues, kPerfTestName); + +} // namespace + +} // namespace krasnopevtseva_v_hoare_batcher_sort From 3555dc54839dde3dda50ae8ab7bf52e6cb05bd4b Mon Sep 17 00:00:00 2001 From: NikaKrasnopevtseva Date: Mon, 23 Feb 2026 10:07:54 +0000 Subject: [PATCH 2/9] fix clang-tidy 1st --- .../seq/include/ops_seq.hpp | 7 ++-- .../seq/src/ops_seq.cpp | 37 ++++++++++--------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp index c1c32e0f..f753c268 100644 --- a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp +++ b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include "krasnopevtseva_v_hoare_batcher_sort/common/include/common.hpp" #include "task/include/task.hpp" @@ -16,9 +17,9 @@ class KrasnopevtsevaVHoareBatcherSortSEQ : public BaseTask { bool ValidationImpl() override; bool PreProcessingImpl() override; bool RunImpl() override; - void compareAndSwap(int &a, int &b); - void batcherMerge(std::vector &arr, int left, int right); - void quickBatcherSort(std::vector &arr, int left, int right); + static void CompareAndSwap(int &a, int &b); + void BatcherMerge(std::vector &arr, int left, int right); + void QuickBatcherSort(std::vector &arr, int left, int right); bool PostProcessingImpl() override; }; diff --git a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp index c61fdf0b..cfe54adc 100644 --- a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp +++ b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp @@ -1,10 +1,11 @@ #include "krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp" #include +#include +#include #include #include "krasnopevtseva_v_hoare_batcher_sort/common/include/common.hpp" -#include "util/include/util.hpp" namespace krasnopevtseva_v_hoare_batcher_sort { @@ -30,19 +31,19 @@ bool KrasnopevtsevaVHoareBatcherSortSEQ::RunImpl() { std::vector sort_v = input; if (size > 1) { - quickBatcherSort(sort_v, 0, size - 1); + QuickBatcherSort(sort_v, 0, static_cast(size - 1)); } GetOutput() = sort_v; return true; } -void KrasnopevtsevaVHoareBatcherSortSEQ::compareAndSwap(int &a, int &b) { +void KrasnopevtsevaVHoareBatcherSortSEQ::CompareAndSwap(int &a, int &b) { if (a > b) { std::swap(a, b); } } -void KrasnopevtsevaVHoareBatcherSortSEQ::batcherMerge(std::vector &arr, int left, int right) { +void KrasnopevtsevaVHoareBatcherSortSEQ::BatcherMerge(std::vector &arr, int left, int right) { int n = right - left + 1; if (n <= 1) { return; @@ -50,36 +51,36 @@ void KrasnopevtsevaVHoareBatcherSortSEQ::batcherMerge(std::vector &arr, int std::vector temp(arr.begin() + left, arr.begin() + right + 1); - std::function oddEvenMerge = [&](int l, int r) { + std::function odd_even_merge = [&](int l, int r) { if (l == r) { return; } - int m = l + (r - l) / 2; - oddEvenMerge(l, m); - oddEvenMerge(m + 1, r); + int m = l + ((r - l) / 2); + odd_even_merge(l, m); + odd_even_merge(m + 1, r); for (int i = l + 1; i + (m - l + 1) <= r; i += 2) { - compareAndSwap(temp[i], temp[i + (m - l + 1)]); + CompareAndSwap(temp[i], temp[i + (m - l + 1)]); } }; - oddEvenMerge(0, n - 1); + odd_even_merge(0, n - 1); for (int i = 1; i + 1 < n; i += 2) { - compareAndSwap(temp[i], temp[i + 1]); + CompareAndSwap(temp[i], temp[i + 1]); } for (int i = 0; i < n; i++) { arr[left + i] = temp[i]; } } -void KrasnopevtsevaVHoareBatcherSortSEQ::quickBatcherSort(std::vector &arr, int left, int right) { +void KrasnopevtsevaVHoareBatcherSortSEQ::QuickBatcherSort(std::vector &arr, int left, int right) { if (left >= right) { return; } - int mid = left + (right - left) / 2; + int mid = left + ((right - left) / 2); if (arr[left] > arr[mid]) { std::swap(arr[left], arr[mid]); } @@ -97,8 +98,8 @@ void KrasnopevtsevaVHoareBatcherSortSEQ::quickBatcherSort(std::vector &arr, int j = right; while (true) { - while (arr[++i] < pivot); - while (arr[--j] > pivot); + while (arr[++i] < pivot){}; + while (arr[--j] > pivot){}; if (i >= j) { break; } @@ -106,11 +107,11 @@ void KrasnopevtsevaVHoareBatcherSortSEQ::quickBatcherSort(std::vector &arr, } std::swap(arr[i], arr[right]); - quickBatcherSort(arr, left, i - 1); - quickBatcherSort(arr, i + 1, right); + QuickBatcherSort(arr, left, i - 1); + QuickBatcherSort(arr, i + 1, right); if (right - left > 32) { - batcherMerge(arr, left, right); + BatcherMerge(arr, left, right); } } bool KrasnopevtsevaVHoareBatcherSortSEQ::PostProcessingImpl() { From 95812f798d9d682047fe51cc000b43fe6c8682e0 Mon Sep 17 00:00:00 2001 From: NikaKrasnopevtseva Date: Mon, 23 Feb 2026 10:14:08 +0000 Subject: [PATCH 3/9] fix clang-tidy 2nd --- .../seq/include/ops_seq.hpp | 1 + .../krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp index f753c268..c95facfe 100644 --- a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp +++ b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp @@ -1,6 +1,7 @@ #pragma once #include + #include "krasnopevtseva_v_hoare_batcher_sort/common/include/common.hpp" #include "task/include/task.hpp" diff --git a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp index cfe54adc..b9d8edb3 100644 --- a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp +++ b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp @@ -98,8 +98,10 @@ void KrasnopevtsevaVHoareBatcherSortSEQ::QuickBatcherSort(std::vector &arr, int j = right; while (true) { - while (arr[++i] < pivot){}; - while (arr[--j] > pivot){}; + while (arr[++i] < pivot) { + }; + while (arr[--j] > pivot) { + }; if (i >= j) { break; } From 7ee76bfccfa50122efa51f1321520a3b19a92daf Mon Sep 17 00:00:00 2001 From: NikaKrasnopevtseva Date: Mon, 23 Feb 2026 10:29:55 +0000 Subject: [PATCH 4/9] fix clang-tidy 3rd --- .../krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp | 2 +- tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp index c95facfe..bf3fa259 100644 --- a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp +++ b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp @@ -19,7 +19,7 @@ class KrasnopevtsevaVHoareBatcherSortSEQ : public BaseTask { bool PreProcessingImpl() override; bool RunImpl() override; static void CompareAndSwap(int &a, int &b); - void BatcherMerge(std::vector &arr, int left, int right); + static void BatcherMerge(std::vector &arr, int left, int right); void QuickBatcherSort(std::vector &arr, int left, int right); bool PostProcessingImpl() override; }; diff --git a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp index b9d8edb3..2eb94ed6 100644 --- a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp +++ b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp @@ -74,7 +74,7 @@ void KrasnopevtsevaVHoareBatcherSortSEQ::BatcherMerge(std::vector &arr, int arr[left + i] = temp[i]; } } - +// NOLINTNEXTLINE(misc-no-recursion) void KrasnopevtsevaVHoareBatcherSortSEQ::QuickBatcherSort(std::vector &arr, int left, int right) { if (left >= right) { return; From 41ad4557a8abfb375cdcf7e3c87a184091b3110a Mon Sep 17 00:00:00 2001 From: NikaKrasnopevtseva Date: Mon, 23 Feb 2026 11:08:39 +0000 Subject: [PATCH 5/9] delete recursive --- - | 0 .../seq/src/ops_seq.cpp | 85 ++++++++++++------- 2 files changed, 55 insertions(+), 30 deletions(-) create mode 100644 - diff --git a/- b/- new file mode 100644 index 00000000..e69de29b diff --git a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp index 2eb94ed6..fb790e13 100644 --- a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp +++ b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -74,43 +75,67 @@ void KrasnopevtsevaVHoareBatcherSortSEQ::BatcherMerge(std::vector &arr, int arr[left + i] = temp[i]; } } -// NOLINTNEXTLINE(misc-no-recursion) + void KrasnopevtsevaVHoareBatcherSortSEQ::QuickBatcherSort(std::vector &arr, int left, int right) { - if (left >= right) { - return; - } + std::stack> stack; + stack.push({left, right}); - int mid = left + ((right - left) / 2); - if (arr[left] > arr[mid]) { - std::swap(arr[left], arr[mid]); - } - if (arr[left] > arr[right]) { - std::swap(arr[left], arr[right]); - } - if (arr[mid] > arr[right]) { - std::swap(arr[mid], arr[right]); - } + while (!stack.empty()) { + auto [l, r] = stack.top(); + stack.pop(); - std::swap(arr[mid], arr[right]); - int pivot = arr[right]; + if (l >= r) { + continue; + } + if (r - l < 16) { + for (int i = l + 1; i <= r; i++) { + int key = arr[i]; + int j = i - 1; + while (j >= l && arr[j] > key) { + arr[j + 1] = arr[j]; + j--; + } + arr[j + 1] = key; + } + continue; + } - int i = left - 1; - int j = right; + int mid = l + (r - l) / 2; + if (arr[l] > arr[mid]) { + std::swap(arr[l], arr[mid]); + } + if (arr[l] > arr[r]) { + std::swap(arr[l], arr[r]); + } + if (arr[mid] > arr[r]) { + std::swap(arr[mid], arr[r]); + } + + std::swap(arr[mid], arr[r - 1]); + int pivot = arr[r - 1]; + + int i = l; + int j = r - 1; - while (true) { - while (arr[++i] < pivot) { - }; - while (arr[--j] > pivot) { - }; - if (i >= j) { - break; + while (true) { + while (arr[++i] < pivot); + while (arr[--j] > pivot); + if (i >= j) { + break; + } + std::swap(arr[i], arr[j]); } - std::swap(arr[i], arr[j]); - } - std::swap(arr[i], arr[right]); - QuickBatcherSort(arr, left, i - 1); - QuickBatcherSort(arr, i + 1, right); + std::swap(arr[i], arr[r - 1]); + + if (i - l < r - i) { + stack.push({i + 1, r}); + stack.push({l, i - 1}); + } else { + stack.push({l, i - 1}); + stack.push({i + 1, r}); + } + } if (right - left > 32) { BatcherMerge(arr, left, right); From 9cb2f98e3418d3b3189ad91cf7e2f4a4d0dc59a0 Mon Sep 17 00:00:00 2001 From: NikaKrasnopevtseva Date: Mon, 23 Feb 2026 18:15:01 +0000 Subject: [PATCH 6/9] cut big function --- - | 0 .../seq/include/ops_seq.hpp | 2 + .../seq/src/ops_seq.cpp | 95 +++++++++++-------- 3 files changed, 56 insertions(+), 41 deletions(-) delete mode 100644 - diff --git a/- b/- deleted file mode 100644 index e69de29b..00000000 diff --git a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp index bf3fa259..4aa80c67 100644 --- a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp +++ b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp @@ -21,6 +21,8 @@ class KrasnopevtsevaVHoareBatcherSortSEQ : public BaseTask { static void CompareAndSwap(int &a, int &b); static void BatcherMerge(std::vector &arr, int left, int right); void QuickBatcherSort(std::vector &arr, int left, int right); + void InsertionSort(std::vector &arr, int left, int right); + int Partition(std::vector &arr, int left, int right); bool PostProcessingImpl() override; }; diff --git a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp index fb790e13..40e7532d 100644 --- a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp +++ b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp @@ -78,7 +78,7 @@ void KrasnopevtsevaVHoareBatcherSortSEQ::BatcherMerge(std::vector &arr, int void KrasnopevtsevaVHoareBatcherSortSEQ::QuickBatcherSort(std::vector &arr, int left, int right) { std::stack> stack; - stack.push({left, right}); + stack.emplace(left, right); while (!stack.empty()) { auto [l, r] = stack.top(); @@ -87,60 +87,73 @@ void KrasnopevtsevaVHoareBatcherSortSEQ::QuickBatcherSort(std::vector &arr, if (l >= r) { continue; } + if (r - l < 16) { - for (int i = l + 1; i <= r; i++) { - int key = arr[i]; - int j = i - 1; - while (j >= l && arr[j] > key) { - arr[j + 1] = arr[j]; - j--; - } - arr[j + 1] = key; - } + InsertionSort(arr, l, r); continue; } - int mid = l + (r - l) / 2; - if (arr[l] > arr[mid]) { - std::swap(arr[l], arr[mid]); - } - if (arr[l] > arr[r]) { - std::swap(arr[l], arr[r]); - } - if (arr[mid] > arr[r]) { - std::swap(arr[mid], arr[r]); - } + int pivotIndex = Partition(arr, l, r); - std::swap(arr[mid], arr[r - 1]); - int pivot = arr[r - 1]; + if (pivotIndex - l < r - pivotIndex) { + stack.emplace(pivotIndex + 1, r); + stack.emplace(l, pivotIndex - 1); + } else { + stack.emplace(l, pivotIndex - 1); + stack.emplace(pivotIndex + 1, r); + } + } - int i = l; - int j = r - 1; + if (right - left > 32) { + BatcherMerge(arr, left, right); + } +} - while (true) { - while (arr[++i] < pivot); - while (arr[--j] > pivot); - if (i >= j) { - break; - } - std::swap(arr[i], arr[j]); +void KrasnopevtsevaVHoareBatcherSortSEQ::InsertionSort(std::vector &arr, int left, int right) { + for (int i = left + 1; i <= right; ++i) { + int key = arr[i]; + int j = i - 1; + while (j >= left && arr[j] > key) { + arr[j + 1] = arr[j]; + --j; } + arr[j + 1] = key; + } +} - std::swap(arr[i], arr[r - 1]); +int KrasnopevtsevaVHoareBatcherSortSEQ::Partition(std::vector &arr, int left, int right) { + int mid = left + ((right - left) / 2); + if (arr[left] > arr[mid]) { + std::swap(arr[left], arr[mid]); + } + if (arr[left] > arr[right]) { + std::swap(arr[left], arr[right]); + } + if (arr[mid] > arr[right]) { + std::swap(arr[mid], arr[right]); + } - if (i - l < r - i) { - stack.push({i + 1, r}); - stack.push({l, i - 1}); - } else { - stack.push({l, i - 1}); - stack.push({i + 1, r}); + std::swap(arr[mid], arr[right - 1]); + int pivot = arr[right - 1]; + + int i = left; + int j = right - 1; + + while (true) { + while (arr[++i] < pivot) { } + while (arr[--j] > pivot) { + } + if (i >= j) { + break; + } + std::swap(arr[i], arr[j]); } - if (right - left > 32) { - BatcherMerge(arr, left, right); - } + std::swap(arr[i], arr[right - 1]); + return i; } + bool KrasnopevtsevaVHoareBatcherSortSEQ::PostProcessingImpl() { return true; } From e001ab878f43cde608b5bf1329cf55b3690d5669 Mon Sep 17 00:00:00 2001 From: NikaKrasnopevtseva Date: Mon, 23 Feb 2026 18:24:11 +0000 Subject: [PATCH 7/9] fix path name & made methods static --- .../seq/include/ops_seq.hpp | 4 ++-- .../seq/src/ops_seq.cpp | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp index 4aa80c67..afdf0290 100644 --- a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp +++ b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp @@ -21,8 +21,8 @@ class KrasnopevtsevaVHoareBatcherSortSEQ : public BaseTask { static void CompareAndSwap(int &a, int &b); static void BatcherMerge(std::vector &arr, int left, int right); void QuickBatcherSort(std::vector &arr, int left, int right); - void InsertionSort(std::vector &arr, int left, int right); - int Partition(std::vector &arr, int left, int right); + static void InsertionSort(std::vector &arr, int left, int right); + static int Partition(std::vector &arr, int left, int right); bool PostProcessingImpl() override; }; diff --git a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp index 40e7532d..f7682438 100644 --- a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp +++ b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/src/ops_seq.cpp @@ -93,14 +93,14 @@ void KrasnopevtsevaVHoareBatcherSortSEQ::QuickBatcherSort(std::vector &arr, continue; } - int pivotIndex = Partition(arr, l, r); + int pivot_index = Partition(arr, l, r); - if (pivotIndex - l < r - pivotIndex) { - stack.emplace(pivotIndex + 1, r); - stack.emplace(l, pivotIndex - 1); + if (pivot_index - l < r - pivot_index) { + stack.emplace(pivot_index + 1, r); + stack.emplace(l, pivot_index - 1); } else { - stack.emplace(l, pivotIndex - 1); - stack.emplace(pivotIndex + 1, r); + stack.emplace(l, pivot_index - 1); + stack.emplace(pivot_index + 1, r); } } From 7e321a34e0f0361f10240e69d68976dbb89c95af Mon Sep 17 00:00:00 2001 From: NikaKrasnopevtseva Date: Mon, 23 Feb 2026 18:33:27 +0000 Subject: [PATCH 8/9] made method static --- .../krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp index afdf0290..fbc53f2e 100644 --- a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp +++ b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/include/ops_seq.hpp @@ -20,7 +20,7 @@ class KrasnopevtsevaVHoareBatcherSortSEQ : public BaseTask { bool RunImpl() override; static void CompareAndSwap(int &a, int &b); static void BatcherMerge(std::vector &arr, int left, int right); - void QuickBatcherSort(std::vector &arr, int left, int right); + static void QuickBatcherSort(std::vector &arr, int left, int right); static void InsertionSort(std::vector &arr, int left, int right); static int Partition(std::vector &arr, int left, int right); bool PostProcessingImpl() override; From d57bcaeca51e354baf70c444fb55512b2fc6a48b Mon Sep 17 00:00:00 2001 From: NikaKrasnopevtseva Date: Tue, 24 Feb 2026 12:52:35 +0000 Subject: [PATCH 9/9] Deleted reports --- tasks/krasnopevtseva_v_hoare_batcher_sort/report.md | 0 tasks/krasnopevtseva_v_hoare_batcher_sort/seq/report.md | 0 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 tasks/krasnopevtseva_v_hoare_batcher_sort/report.md delete mode 100644 tasks/krasnopevtseva_v_hoare_batcher_sort/seq/report.md diff --git a/tasks/krasnopevtseva_v_hoare_batcher_sort/report.md b/tasks/krasnopevtseva_v_hoare_batcher_sort/report.md deleted file mode 100644 index e69de29b..00000000 diff --git a/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/report.md b/tasks/krasnopevtseva_v_hoare_batcher_sort/seq/report.md deleted file mode 100644 index e69de29b..00000000