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
# 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 \

View File

@ -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))

View File

@ -1,7 +1,6 @@
#include <fmt/format.h>
#include <QCoreApplication>
#include <QFile>
#include <vector>
#include <fstream>
#include <mergesort.h>
@ -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<int32_t> dataset{10, 3, 244, 23, 293, 2393, 302};
// std::vector<int32_t> 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<int32_t> 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;
}

View File

@ -8,6 +8,7 @@ namespace algo {
template<typename Iterator, typename Comparator>
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<typename Container, typename Iterator, typename Comparator>
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<typename Iterator, typename Comparator>
void mergesort(Iterator start, Iterator end, Comparator cmp) {
using valtype = typename std::iterator_traits<Iterator>::value_type;
std::vector<valtype> output_vec(std::distance(start, end));
std::vector<valtype> temporary_dataset(std::distance(start, end));
ms_split(temporary_dataset, start, end - 1, cmp, temporary_dataset.begin());
}
}