From 07bb26917581e9fa0831e631db75895b4e504089 Mon Sep 17 00:00:00 2001 From: robtor Date: Wed, 1 Nov 2023 10:00:36 +0100 Subject: [PATCH] Make mergesort working --- docker/Dockerfile | 3 +- task1/dataset-gen.py | 2 +- task1/main.cpp | 77 ++++++++++++++++++++++++-------------------- task1/mergesort.h | 42 +++++++++++++----------- 4 files changed, 67 insertions(+), 57 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 4599793..87d93de 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -3,8 +3,7 @@ FROM ubuntu:${UBUNTU_VERSION} # Build environment # Profiling and analyzer tools -RUN --mount=type=bind,target=/var/lib/apt \ - apt-get -y update \ +RUN apt-get -y update \ && DEBIAN_FRONTEND=noninteractive TZ=Europe/Berlin apt-get -y install tzdata apt-utils \ && apt-get -y upgrade \ && apt-get -y install python3 python3-pip lsb-release software-properties-common \ diff --git a/task1/dataset-gen.py b/task1/dataset-gen.py index 59d4064..c7ec27c 100644 --- a/task1/dataset-gen.py +++ b/task1/dataset-gen.py @@ -8,7 +8,7 @@ print("Generated random vector with {} entries".format(randvec.size)) with open("dataset.dat", "w") as file: for value in randvec: - file.write("{}\n".format(value)) + file.write(str(value) + '\n') fsize = os.path.getsize("dataset.dat") print("File written down with Size {} MB".format(fsize/1000/1000)) \ No newline at end of file diff --git a/task1/main.cpp b/task1/main.cpp index e4015d9..8027720 100644 --- a/task1/main.cpp +++ b/task1/main.cpp @@ -1,7 +1,6 @@ #include -#include -#include #include +#include #include @@ -22,40 +21,48 @@ your predictions and their graphical illustration? */ int main(int argc, char *argv[]) { - QFile file("dataset.dat"); - if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + try { + std::ifstream file("dataset.dat", std::ios_base::in); + + fmt::print("Opened file {} sucessfully!\n", "dummy"); + + std::vector dataset{10, 3, 244, 23, 293, 2393, 302}; + +// std::vector dataset; +// int counter = 0; +// +// while(!file.eof()) { +// int32_t buf; +// file >> buf; +// dataset.emplace_back(buf); +// } +// +// fmt::print("Read {} values from {}\n", dataset.size(), "dummy"); + + algo::mergesort(dataset.begin(), dataset.end(), [](int32_t a, int32_t b) { + return (a>b); + }); + + fmt::print("sorted"); + +// 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; + + } catch (std::exception e) { fmt::print("Could not open file"); - return 0; + return -1; } - fmt::print("Opened file {} sucessfully!\n", file.fileName().toStdString()); - - - std::vector dataset; - int counter = 0; - - while(!file.atEnd()) { - dataset.emplace_back(file.readLine().toInt()); - } - - 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.h b/task1/mergesort.h index b6aecbb..da5a2a7 100644 --- a/task1/mergesort.h +++ b/task1/mergesort.h @@ -8,6 +8,7 @@ namespace algo { template void merge(Iterator start, Iterator middle, Iterator end, Comparator cmp, Iterator output_start) { Iterator start_m = start; + Iterator begin = output_start; Iterator start2 = middle + 1; //merge from input until one half completes @@ -35,37 +36,40 @@ namespace algo { 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; + const auto size = std::distance(start_m, end); + for (auto i = 0; i <= size; i++, start_m++, begin++) { + *start_m = *begin; } } 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; + void ms_split(Container &output_vec, Iterator start, Iterator end, Comparator cmp, Iterator output_start) { + Iterator mid = start; + Iterator begin = 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); + if (std::distance(start, end) < 1) { + return; + } else { + //move mid iterator litterally to the mid + std::advance(mid, std::distance(start, end) / 2); + //sort the first half within an recursion + ms_split(output_vec, start, mid, 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); + //move output iterator + std::advance(output_start, std::distance(start, mid + 1)); + //sort the second half within a recursion + ms_split(output_vec, mid + 1, end, cmp, output_start); - //merge everything together starting from the complete beginning - merge(start, middle, end, cmp, output_started_from); + //merge everything together starting from the complete beginning + merge(start, mid, end, cmp, begin); + } } 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)); - + std::vector temporary_dataset(std::distance(start, end)); + ms_split(temporary_dataset, start, end - 1, cmp, temporary_dataset.begin()); } } \ No newline at end of file