Make mergesort working

This commit is contained in:
Robin Dietzel 2023-11-01 10:00:36 +01:00
parent b744c5489d
commit 07bb269175
4 changed files with 67 additions and 57 deletions

View File

@ -3,8 +3,7 @@ FROM ubuntu:${UBUNTU_VERSION}
# Build environment # Build environment
# Profiling and analyzer tools # Profiling and analyzer tools
RUN --mount=type=bind,target=/var/lib/apt \ RUN apt-get -y update \
apt-get -y update \
&& DEBIAN_FRONTEND=noninteractive TZ=Europe/Berlin apt-get -y install tzdata apt-utils \ && DEBIAN_FRONTEND=noninteractive TZ=Europe/Berlin apt-get -y install tzdata apt-utils \
&& apt-get -y upgrade \ && apt-get -y upgrade \
&& apt-get -y install python3 python3-pip lsb-release software-properties-common \ && apt-get -y install python3 python3-pip lsb-release software-properties-common \

View File

@ -8,7 +8,7 @@ print("Generated random vector with {} entries".format(randvec.size))
with open("dataset.dat", "w") as file: with open("dataset.dat", "w") as file:
for value in randvec: for value in randvec:
file.write("{}\n".format(value)) file.write(str(value) + '\n')
fsize = os.path.getsize("dataset.dat") fsize = os.path.getsize("dataset.dat")
print("File written down with Size {} MB".format(fsize/1000/1000)) print("File written down with Size {} MB".format(fsize/1000/1000))

View File

@ -1,7 +1,6 @@
#include <fmt/format.h> #include <fmt/format.h>
#include <QCoreApplication>
#include <QFile>
#include <vector> #include <vector>
#include <fstream>
#include <mergesort.h> #include <mergesort.h>
@ -22,40 +21,48 @@ your predictions and their graphical illustration?
*/ */
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
QFile file("dataset.dat"); try {
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) { std::ifstream file("dataset.dat", std::ios_base::in);
fmt::print("Could not open file");
return 0;
}
fmt::print("Opened file {} sucessfully!\n", file.fileName().toStdString()); fmt::print("Opened file {} sucessfully!\n", "dummy");
std::vector<int32_t> dataset{10, 3, 244, 23, 293, 2393, 302};
std::vector<int32_t> dataset; // std::vector<int32_t> dataset;
int counter = 0; // int counter = 0;
//
while(!file.atEnd()) { // while(!file.eof()) {
dataset.emplace_back(file.readLine().toInt()); // int32_t buf;
} // file >> buf;
// dataset.emplace_back(buf);
fmt::print("Read {} values from {}\n", dataset.size(), file.fileName().toStdString()); // }
//
// fmt::print("Read {} values from {}\n", dataset.size(), "dummy");
algo::mergesort(dataset.begin(), dataset.end(), [](int32_t a, int32_t b) { algo::mergesort(dataset.begin(), dataset.end(), [](int32_t a, int32_t b) {
return (a>b); return (a>b);
}); });
QFile outfile("dataset-sorted.dat"); fmt::print("sorted");
if(!outfile.open(QIODevice::WriteOnly | QIODevice::Text)) {
fmt::print("Error! Could not create output file");
}
for(auto &val : dataset) { // QFile outfile("dataset-sorted.dat");
outfile.write(fmt::format("{}\n", val).c_str()); // if(!outfile.open(QIODevice::WriteOnly | QIODevice::Text)) {
} // fmt::print("Error! Could not create output file");
// }
file.close(); //
outfile.flush(); // for(auto &val : dataset) {
outfile.close(); // outfile.write(fmt::format("{}\n", val).c_str());
// }
//
// file.close();
// outfile.flush();
// outfile.close();
return 0; return 0;
} catch (std::exception e) {
fmt::print("Could not open file");
return -1;
}
} }

View File

@ -8,6 +8,7 @@ namespace algo {
template<typename Iterator, typename Comparator> template<typename Iterator, typename Comparator>
void merge(Iterator start, Iterator middle, Iterator end, Comparator cmp, Iterator output_start) { void merge(Iterator start, Iterator middle, Iterator end, Comparator cmp, Iterator output_start) {
Iterator start_m = start; Iterator start_m = start;
Iterator begin = output_start;
Iterator start2 = middle + 1; Iterator start2 = middle + 1;
//merge from input until one half completes //merge from input until one half completes
@ -35,37 +36,40 @@ namespace algo {
output_start++; output_start++;
} }
const auto nit = std::distance(start_m, end); const auto size = std::distance(start_m, end);
for (auto i = 0; i <= nit; i++, start_m++, output_start++) { for (auto i = 0; i <= size; i++, start_m++, begin++) {
*start_m = *output_start; *start_m = *begin;
} }
} }
template<typename Container, typename Iterator, typename Comparator> template<typename Container, typename Iterator, typename Comparator>
void mergesort_splitup(Container &output_vec, Iterator start, Iterator end, Comparator cmp, Iterator output_start) { void ms_split(Container &output_vec, Iterator start, Iterator end, Comparator cmp, Iterator output_start) {
Iterator middle = start; Iterator mid = start;
Iterator output_started_from = output_start; Iterator begin = output_start;
//move middle iterator litterally to the middle if (std::distance(start, end) < 1) {
std::advance(middle, std::distance(start, end) / 2); return;
} else {
//move mid iterator litterally to the mid
std::advance(mid, std::distance(start, end) / 2);
//sort the first half within an recursion //sort the first half within an recursion
mergesort_splitup(output_vec, start, middle, cmp, output_start); ms_split(output_vec, start, mid, cmp, output_start);
//move output iterator //move output iterator
std::advance(output_start, std::distance(start, middle + 1)); std::advance(output_start, std::distance(start, mid + 1));
//sort the second half within a recursion //sort the second half within a recursion
mergesort_splitup(output_vec, middle + 1, end, cmp, output_start); ms_split(output_vec, mid + 1, end, cmp, output_start);
//merge everything together starting from the complete beginning //merge everything together starting from the complete beginning
merge(start, middle, end, cmp, output_started_from); merge(start, mid, end, cmp, begin);
}
} }
template<typename Iterator, typename Comparator> template<typename Iterator, typename Comparator>
void mergesort(Iterator start, Iterator end, Comparator cmp) { void mergesort(Iterator start, Iterator end, Comparator cmp) {
using valtype = typename std::iterator_traits<Iterator>::value_type; using valtype = typename std::iterator_traits<Iterator>::value_type;
std::vector<valtype> temporary_dataset(std::distance(start, end));
std::vector<valtype> output_vec(std::distance(start, end)); ms_split(temporary_dataset, start, end - 1, cmp, temporary_dataset.begin());
} }
} }