diff --git a/3sem/data structures and algos/05/program.cpp b/3sem/data structures and algos/05/program.cpp new file mode 100644 index 0000000..4866e10 --- /dev/null +++ b/3sem/data structures and algos/05/program.cpp @@ -0,0 +1,96 @@ +// dsaa_05.cpp +// Горбацевич Андрей +#include +#include +#include +#include +#include +#include + + +inline void time_passed(std::chrono::system_clock::time_point start, double &holder) { + auto stop = std::chrono::high_resolution_clock::now(); + auto duration = std::chrono::duration_cast(stop - start); + holder = duration.count(); +} + +typedef double num_type; +typedef std::vector num_list; + +void test(); +void algo_01(num_list &cont); // Stupid sort +void algo_02(num_list &cont); // Shell sort + + +int main() { + test(); + + return 0; +} + +void test() { + int n; + std::cout << "Count of items:\n"; + std::cin >> n; + num_list data; + data.reserve(n); + for (int i = 0; i < n; i++) { + int d; + std::cin >> d; + data.push_back(d); + } + + + num_list copy_1(data); + num_list copy_2(data); + + double fft, sft = fft = 0; + + { + auto start = std::chrono::high_resolution_clock::now(); + algo_01(copy_1); + time_passed(start, fft); + } + { + auto start = std::chrono::high_resolution_clock::now(); + algo_02(copy_2); + time_passed(start, sft); + } + { + assert(copy_1 == copy_2); + } + + printf( + "algo_01 (%.0f) is %s than algo_02 (%.0f) by %.0f microseconds\n", + fft, (fft > sft? "slower" : "faster"), sft, fabs(fft - sft) + ); +} + +void algo_01(num_list &cont) { // Stupid sort + auto vit_b = cont.begin() + 1; + + while (vit_b != cont.end()) { + auto vit_p = vit_b - 1; + if (*vit_p > *vit_b) { + std::swap(*vit_p, *vit_b); + vit_b = cont.begin() + 1; + } + else { + vit_b += 1; + } + } +} + +void algo_02(num_list &cont) { // Shell sort + auto vit_first = cont.begin(); + auto vit_last = cont.end(); + for(auto d = (vit_last - vit_first) / 2; d != 0; d /= 2) { + for(auto i = vit_first + d; i != vit_last; ++i) { + for(auto j = i; j - vit_first >= d; j -= d) { + if (*j < *(j - d)) { + std::swap( *j, *( j - d ) ); + } + } + } + } +} diff --git a/3sem/data structures and algos/05/protecc.cpp b/3sem/data structures and algos/05/protecc.cpp new file mode 100644 index 0000000..5ba81fb --- /dev/null +++ b/3sem/data structures and algos/05/protecc.cpp @@ -0,0 +1,110 @@ +// dsaa_05.cpp +// Горбацевич Андрей +#include +#include +#include +#include +#include +#include +#include + + +inline void time_passed(std::chrono::system_clock::time_point start, double &holder) { + auto stop = std::chrono::high_resolution_clock::now(); + auto duration = std::chrono::duration_cast(stop - start); + holder = duration.count(); +} + +typedef double num_type; +typedef std::vector num_list; + +void test(); +void algo_01(num_list &cont); // Stupid sort +void algo_02(num_list &cont); // Shell sort + + +int main() { + test(); + + return 0; +} + +void test() { + int n; + std::cout << "Count of items:\n"; + std::cin >> n; + num_list data; + data.reserve(n); + for (int i = 0; i < n; i++) { + int d; + std::cin >> d; + data.push_back(d); + } + + + num_list copy_1(data); + num_list copy_2(data); + + double fft, sft = fft = 0; + + { + auto start = std::chrono::high_resolution_clock::now(); + algo_01(copy_1); + time_passed(start, fft); + } + { + auto start = std::chrono::high_resolution_clock::now(); + algo_02(copy_2); + time_passed(start, sft); + } + { + assert(copy_1 == copy_2); + } + + printf( + "algo_01 (%.0f) is %s than algo_02 (%.0f) by %.0f microseconds\n", + fft, (fft > sft? "slower" : "faster"), sft, fabs(fft - sft) + ); +} + +void algo_01(num_list &cont) { // Stupid sort + auto vit_b = cont.begin() + 1; + + while (vit_b != cont.end()) { + auto vit_p = vit_b - 1; + if (*vit_p > *vit_b) { + std::swap(*vit_p, *vit_b); + vit_b = cont.begin() + 1; + } + else { + vit_b += 1; + } + } +} + +void algo_02(num_list &cont) { // qSort sort + std::function choose_mid = [](num_list &cont, int l, int r) { + int v = cont[r]; + int pivot = l - 1; + + for (int i = l; i <= r - 1; i++) { + if (cont[i] <= v) { + pivot += 1; + std::swap(cont[pivot], cont[i]); + } + } + std::swap(cont[pivot+1], cont[r]); + + return pivot + 1; + }; + + std::function q_sort = [&choose_mid, &q_sort](num_list &cont, int l, int r) { + if (l < r) { + int mid = choose_mid(cont, l, r); + q_sort(cont, l, mid - 1); + q_sort(cont, mid + 1, r); + } + }; + + q_sort(cont, 0, cont.size()-1); +} diff --git a/3sem/data structures and algos/05/аисд отчёт прак 5.pdf b/3sem/data structures and algos/05/аисд отчёт прак 5.pdf new file mode 100644 index 0000000..aca0c13 Binary files /dev/null and b/3sem/data structures and algos/05/аисд отчёт прак 5.pdf differ