diff --git a/docker/Dockerfile b/docker/Dockerfile index edafadb..4599793 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -27,5 +27,4 @@ RUN env; if [[ ${UBUNTU_VERSION} == "23.04" || ${UBUNTU_VERSION} == "23.10" ]]; pip3 install conan==${CONAN_VERSION}; fi ARG GCC_VERSION="13" -RUN apt-get -y install gcc-${GCC_VERSION} g++-${GCC_VERSION} - +RUN apt-get -y install gcc-${GCC_VERSION} g++-${GCC_VERSION} \ No newline at end of file diff --git a/task1/CMakeLists.txt b/task1/CMakeLists.txt index c8b3947..6f70ae5 100644 --- a/task1/CMakeLists.txt +++ b/task1/CMakeLists.txt @@ -18,7 +18,9 @@ add_custom_target(task1_7_dataset ${CMAKE_CURRENT_SOURCE_DIR}/dataset.dat ${CMAKE_CURRENT_BINARY_DIR}/dataset.dat) -add_executable(task1_7 main.cpp) +add_executable(task1_7 main.cpp + mergesort.h) + target_link_libraries(task1_7 fmt::fmt Qt::Core) diff --git a/task1/main.cpp b/task1/main.cpp index 5c1d909..e4015d9 100644 --- a/task1/main.cpp +++ b/task1/main.cpp @@ -2,7 +2,8 @@ #include #include #include -#include + +#include /* Create a simple sorting application that uses the mergesort algorithm to sort a @@ -23,7 +24,7 @@ your predictions and their graphical illustration? int main(int argc, char *argv[]) { QFile file("dataset.dat"); if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) { - //qDebug() << "Could not open file"; + fmt::print("Could not open file"); return 0; } @@ -39,5 +40,22 @@ int main(int argc, char *argv[]) { fmt::print("Read {} values from {}\n", dataset.size(), file.fileName().toStdString()); + algo::mergesort(dataset.begin(), dataset.end(), [](int32_t a, int32_t b) { + return (a>b); + }); + + QFile outfile("dataset-sorted.dat"); + if(!outfile.open(QIODevice::WriteOnly | QIODevice::Text)) { + fmt::print("Error! Could not create output file"); + } + + for(auto &val : dataset) { + outfile.write(fmt::format("{}\n", val).c_str()); + } + + file.close(); + outfile.flush(); + outfile.close(); + return 0; } diff --git a/task1/mergesort.cpp b/task1/mergesort.cpp new file mode 100644 index 0000000..e69de29 diff --git a/task1/mergesort.h b/task1/mergesort.h new file mode 100644 index 0000000..b6aecbb --- /dev/null +++ b/task1/mergesort.h @@ -0,0 +1,71 @@ +#pragma once + +#include +#include + +namespace algo { + + template + void merge(Iterator start, Iterator middle, Iterator end, Comparator cmp, Iterator output_start) { + Iterator start_m = start; + Iterator start2 = middle + 1; + + //merge from input until one half completes + while (start <= middle && start2 <= end) { + if (cmp(*start, *start2)) { + *output_start = *start; + start++; + } else { + *output_start = *start2; + start2++; + } + output_start++; + } + + //try to finish first half + while (start <= middle) { + *output_start = *start; + start++; + output_start++; + } + + while (start2 <= end) { + *output_start = *start2; + start2++; + output_start++; + } + + const auto nit = std::distance(start_m, end); + for (auto i = 0; i <= nit; i++, start_m++, output_start++) { + *start_m = *output_start; + } + + } + + template + void mergesort_splitup(Container &output_vec, Iterator start, Iterator end, Comparator cmp, Iterator output_start) { + Iterator middle = start; + Iterator output_started_from = output_start; + + //move middle iterator litterally to the middle + std::advance(middle, std::distance(start, end) / 2); + //sort the first half within an recursion + mergesort_splitup(output_vec, start, middle, cmp, output_start); + + //move output iterator + std::advance(output_start, std::distance(start, middle + 1)); + //sort the second half within a recursion + mergesort_splitup(output_vec, middle + 1, end, cmp, output_start); + + //merge everything together starting from the complete beginning + merge(start, middle, end, cmp, output_started_from); + } + + template + void mergesort(Iterator start, Iterator end, Comparator cmp) { + using valtype = typename std::iterator_traits::value_type; + + std::vector output_vec(std::distance(start, end)); + + } +} \ No newline at end of file