|
| 1 | +// |
| 2 | +// algorithm - some algorithms in "Introduction to Algorithms", third edition |
| 3 | +// Copyright (C) 2018 lxylxy123456 |
| 4 | +// |
| 5 | +// This program is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU Affero General Public License as |
| 7 | +// published by the Free Software Foundation, either version 3 of the |
| 8 | +// License, or (at your option) any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU Affero General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU Affero General Public License |
| 16 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | +// |
| 18 | + |
| 19 | +#ifndef MAIN |
| 20 | +#define MAIN |
| 21 | +#define MAIN_PMergeSort |
| 22 | +#endif |
| 23 | + |
| 24 | +#ifndef FUNC_PMergeSort |
| 25 | +#define FUNC_PMergeSort |
| 26 | + |
| 27 | +#include <thread> |
| 28 | +#include "utils.h" |
| 29 | + |
| 30 | +#include "MergeSort.cpp" |
| 31 | + |
| 32 | +template <typename T> |
| 33 | +void MergeSort_prime(std::vector<T>* A, size_t p, size_t r) { |
| 34 | + if (p < r - 1) { |
| 35 | + size_t q = (p + r) / 2; |
| 36 | + std::thread t1(MergeSort_prime<T>, A, p, q); |
| 37 | + MergeSort_prime(A, q, r); |
| 38 | + t1.join(); |
| 39 | + Merge(*A, p, q, r); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +template <typename T> |
| 44 | +void MergeSort_prime(std::vector<T>& A) { |
| 45 | + MergeSort_prime(&A, 0, A.size()); |
| 46 | +} |
| 47 | + |
| 48 | +template <typename T> |
| 49 | +size_t BinarySearch(T x, std::vector<T>& S, size_t p, size_t r) { |
| 50 | + size_t low = p; |
| 51 | + size_t high = std::max(p, r); |
| 52 | + while (low < high) { |
| 53 | + size_t mid = (low + high + 1) / 2; |
| 54 | + if (x <= S[mid - 1]) |
| 55 | + high = mid - 1; |
| 56 | + else |
| 57 | + low = mid; |
| 58 | + } |
| 59 | + return low; |
| 60 | +} |
| 61 | + |
| 62 | +template <typename T> |
| 63 | +void PMerge(std::vector<T>* S, size_t p1, size_t r1, size_t p2, size_t r2, |
| 64 | + std::vector<T>* A, size_t p3) { |
| 65 | + size_t n1 = r1 - p1; |
| 66 | + size_t n2 = r2 - p2; |
| 67 | + if (n1 < n2) { |
| 68 | + std::swap(p1, p2); |
| 69 | + std::swap(r1, r2); |
| 70 | + std::swap(n1, n2); |
| 71 | + } |
| 72 | + if (!n1) |
| 73 | + return; |
| 74 | + size_t q1 = (p1 + r1) / 2; |
| 75 | + size_t q2 = BinarySearch((*S)[q1], *S, p2, r2); |
| 76 | + size_t q3 = p3 + (q1 - p1) + (q2 - p2); |
| 77 | + (*A)[q3] = (*S)[q1]; |
| 78 | + std::thread t1(PMerge<T>, S, p1, q1, p2, q2, A, p3); |
| 79 | + PMerge(S, q1 + 1, r1, q2, r2, A, q3 + 1); |
| 80 | + t1.join(); |
| 81 | +} |
| 82 | + |
| 83 | +template <typename T> |
| 84 | +void PMergeSort(std::vector<T>* A, size_t p, size_t r, std::vector<T>* B, |
| 85 | + size_t s) { |
| 86 | + size_t n = r - p; |
| 87 | + if (n == 1) |
| 88 | + (*B)[s] = (*A)[p]; |
| 89 | + else { |
| 90 | + std::vector<T> S(n); |
| 91 | + size_t q = (p + r) / 2; |
| 92 | + size_t qq = q - p; |
| 93 | + std::thread t1(PMergeSort<T>, A, p, q, &S, 0); |
| 94 | + PMergeSort(A, q, r, &S, qq); |
| 95 | + t1.join(); |
| 96 | + PMerge(&S, 0, qq, qq, n, B, s); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +template <typename T> |
| 101 | +void PMergeSort(std::vector<T>& A, std::vector<T>& B) { |
| 102 | + PMergeSort(&A, 0, A.size(), &B, 0); |
| 103 | +} |
| 104 | +#endif |
| 105 | + |
| 106 | +#ifdef MAIN_PMergeSort |
| 107 | +int main(int argc, char *argv[]) { |
| 108 | + std::vector<int> a; |
| 109 | + int n = get_argv(argc, argv, 1, 10); |
| 110 | + random_integers(a, 0, n - 1, n); |
| 111 | + output_integers(a); |
| 112 | + std::vector<int> b(a), c(n); |
| 113 | + MergeSort_prime(b); |
| 114 | + output_integers(b); |
| 115 | + PMergeSort(a, c); |
| 116 | + output_integers(c); |
| 117 | + std::cout << std::boolalpha << (b == c) << std::endl; |
| 118 | + return 0; |
| 119 | +} |
| 120 | +#endif |
| 121 | + |
0 commit comments