| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "timofeev_n_radix_merge_sort/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <ranges> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "timofeev_n_radix_merge_sort/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace timofeev_n_radix_merge_sort { | ||
| 12 | |||
| 13 |
1/2✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
|
88 | TimofeevNRadixMergeSEQ::TimofeevNRadixMergeSEQ(const InType &in) { |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 15 |
1/2✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
|
88 | GetInput() = in; |
| 16 | 88 | GetOutput() = std::vector<int>(0); | |
| 17 | 88 | } | |
| 18 | |||
| 19 | 88 | bool TimofeevNRadixMergeSEQ::ValidationImpl() { | |
| 20 | 88 | return !GetInput().empty(); | |
| 21 | } | ||
| 22 | |||
| 23 | 88 | bool TimofeevNRadixMergeSEQ::PreProcessingImpl() { | |
| 24 | 88 | return true; | |
| 25 | } | ||
| 26 | |||
| 27 | ✗ | int TimofeevNRadixMergeSEQ::GetDigit(int num, int digit) { | |
| 28 | 304 | int abs_num = std::abs(num); | |
| 29 | |||
| 30 | // потому что без использования возведения в степень (как функции) | ||
| 31 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 304 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
304 | for (int i = 0; i < digit; i++) { |
| 32 | ✗ | abs_num /= 10; | |
| 33 | } | ||
| 34 | |||
| 35 | 304 | return abs_num % 10; | |
| 36 | } | ||
| 37 | |||
| 38 |
1/2✓ Branch 0 taken 88 times.
✗ Branch 1 not taken.
|
88 | int TimofeevNRadixMergeSEQ::GetMaxDigits(const std::vector<int> &arr) { |
| 39 |
1/2✓ Branch 0 taken 88 times.
✗ Branch 1 not taken.
|
88 | if (arr.empty()) { |
| 40 | return 0; | ||
| 41 | } | ||
| 42 | |||
| 43 | // Находим максимальное по модулю число | ||
| 44 | 88 | int max_abs = 0; | |
| 45 |
2/2✓ Branch 0 taken 304 times.
✓ Branch 1 taken 88 times.
|
392 | for (int num : arr) { |
| 46 | 304 | int abs_num = std::abs(num); | |
| 47 | 304 | max_abs = std::max(abs_num, max_abs); | |
| 48 | } | ||
| 49 | |||
| 50 | int digits = 0; | ||
| 51 |
2/2✓ Branch 0 taken 80 times.
✓ Branch 1 taken 88 times.
|
168 | while (max_abs > 0) { |
| 52 | 80 | digits++; | |
| 53 | 80 | max_abs /= 10; | |
| 54 | } | ||
| 55 | 88 | return digits == 0 ? 1 : digits; // минимум 1 разряд | |
| 56 | } | ||
| 57 | |||
| 58 | 72 | void TimofeevNRadixMergeSEQ::SplitPosNeg(const std::vector<int> &arr, std::vector<int> &negative, | |
| 59 | std::vector<int> &positive) { | ||
| 60 |
2/2✓ Branch 0 taken 304 times.
✓ Branch 1 taken 72 times.
|
376 | for (int num : arr) { |
| 61 |
2/2✓ Branch 0 taken 136 times.
✓ Branch 1 taken 168 times.
|
304 | if (num < 0) { |
| 62 | 136 | negative.push_back(-num); | |
| 63 | } else { | ||
| 64 | positive.push_back(num); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | 72 | } | |
| 68 | |||
| 69 | 88 | void TimofeevNRadixMergeSEQ::RadixMergeBucketHelpingFunction(std::vector<int> &part, int digit) { | |
| 70 | 88 | std::vector<std::vector<int>> buckets(10); | |
| 71 |
2/2✓ Branch 0 taken 304 times.
✓ Branch 1 taken 88 times.
|
392 | for (int num : part) { |
| 72 | int d = GetDigit(num, digit); | ||
| 73 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 304 times.
|
304 | buckets[d].push_back(num); |
| 74 | } | ||
| 75 | |||
| 76 | // Собираем числа обратно в вектор | ||
| 77 | part.clear(); | ||
| 78 |
2/2✓ Branch 0 taken 880 times.
✓ Branch 1 taken 88 times.
|
968 | for (int i = 0; i < 10; i++) { |
| 79 |
3/4✓ Branch 0 taken 304 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 304 times.
✓ Branch 3 taken 880 times.
|
1184 | for (int num : buckets[i]) { |
| 80 | part.push_back(num); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | 88 | } | |
| 84 | |||
| 85 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 72 times.
|
88 | void TimofeevNRadixMergeSEQ::RadixMergeSort(std::vector<int> &part) { |
| 86 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 72 times.
|
88 | if (part.size() <= 1) { |
| 87 | 16 | return; | |
| 88 | } | ||
| 89 | |||
| 90 | // Разделяем на отрицательные и положительные числа | ||
| 91 | 72 | std::vector<int> negative; | |
| 92 | 72 | std::vector<int> positive; | |
| 93 |
1/2✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
|
72 | SplitPosNeg(part, negative, positive); |
| 94 | |||
| 95 | // Сортируем отрицательные числа (по модулю) | ||
| 96 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 32 times.
|
72 | if (!negative.empty()) { |
| 97 | 40 | int max_digits_neg = GetMaxDigits(negative); | |
| 98 | |||
| 99 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 40 times.
|
80 | for (int digit = 0; digit < max_digits_neg; digit++) { |
| 100 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | RadixMergeBucketHelpingFunction(negative, digit); |
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | // Сортируем положительные числа | ||
| 105 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
|
72 | if (!positive.empty()) { |
| 106 | 48 | int max_digits_pos = GetMaxDigits(positive); | |
| 107 | |||
| 108 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 48 times.
|
96 | for (int digit = 0; digit < max_digits_pos; digit++) { |
| 109 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | RadixMergeBucketHelpingFunction(positive, digit); |
| 110 | } | ||
| 111 | } | ||
| 112 | // А теперь сливаем всё в один вектор, причём негативные - задом наперёд, | ||
| 113 | // позитивные - как было. | ||
| 114 | // Вот так. | ||
| 115 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 32 times.
|
72 | if (!negative.empty()) { |
| 116 | int j = 0; | ||
| 117 |
2/2✓ Branch 0 taken 136 times.
✓ Branch 1 taken 40 times.
|
176 | for (int &i : std::ranges::reverse_view(negative)) { |
| 118 | 136 | part[j] = -i; | |
| 119 | 136 | j++; | |
| 120 | } | ||
| 121 | } | ||
| 122 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
|
72 | if (!positive.empty()) { |
| 123 |
2/2✓ Branch 0 taken 168 times.
✓ Branch 1 taken 48 times.
|
216 | for (size_t i = 0; i < positive.size(); i++) { |
| 124 | 168 | part[i + negative.size()] = positive[i]; | |
| 125 | } | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | 88 | bool TimofeevNRadixMergeSEQ::RunImpl() { | |
| 130 | 88 | GetOutput().resize(GetInput().size()); | |
| 131 |
2/2✓ Branch 0 taken 320 times.
✓ Branch 1 taken 88 times.
|
408 | for (size_t i = 0; i < GetInput().size(); i++) { |
| 132 | 320 | GetOutput()[i] = GetInput()[i]; | |
| 133 | } | ||
| 134 | 88 | RadixMergeSort(GetOutput()); | |
| 135 | 88 | return true; | |
| 136 | } | ||
| 137 | |||
| 138 | 88 | bool TimofeevNRadixMergeSEQ::PostProcessingImpl() { | |
| 139 | 88 | return true; | |
| 140 | } | ||
| 141 | |||
| 142 | } // namespace timofeev_n_radix_merge_sort | ||
| 143 |