This commit is contained in:
karl 2020-10-18 11:25:59 +02:00
commit b48675a931
5 changed files with 59 additions and 57 deletions

View File

@ -1,6 +1,8 @@
#include <stdio.h> #include <stdio.h>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <iomanip> // setw for padleft
#include "Timing.h" #include "Timing.h"
Timing* Timing::mInstance = 0; Timing* Timing::mInstance = 0;
@ -52,19 +54,20 @@ void Timing::stopRecord(const std::string& name) {
* Set prettyPrint to true to display mm:ss.ms instead of ms. * Set prettyPrint to true to display mm:ss.ms instead of ms.
*/ */
void Timing::print(const bool prettyPrint) const { void Timing::print(const bool prettyPrint) const {
std::cout << "-----" << std::endl << "Results: " << std::endl << "-----" << std::endl; std::cout << "--------------------" << std::endl << "Results: " << std::endl << "--------------------" << std::endl;
std::string blub;
auto it = mResults.begin(); auto it = mResults.begin();
while(it != mResults.end()) { while(it != mResults.end()) {
if (prettyPrint) { if (prettyPrint) {
std::cout << it->first << ": " << parseDate((int) it->second.count()) << std::endl; std::cout << it->first << ": " << parseDate((int) it->second.count()) << std::endl;
} else { } else {
std::cout << it->first << ": " << it->second.count() << "ms" << std::endl; std::cout << it->first << ": " << std::setfill(' ') << std::setw(10) << std::fixed << std::setprecision(4) << it->second.count() << "ms" << std::endl;
} }
it++; it++;
} }
std::cout << "-----" << std::endl; std::cout << "--------------------" << std::endl;
} }
/** /**

View File

@ -17,7 +17,7 @@ uint32_t getWirthKthSmallest(std::vector<uint32_t> a, uint32_t k)
while (x < a[j]) j--; while (x < a[j]) j--;
if (i <= j) { if (i <= j) {
std::swap(a[i], a[j]); swap(&a[i], &a[j]);
i++; i++;
j--; j--;
} }

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
// custom swap function // custom swap function for just pointer swapping (check difference)
void swap(uint32_t* a, uint32_t* b) void swap(uint32_t* a, uint32_t* b)
{ {
uint32_t temp = *a; uint32_t temp = *a;

View File

@ -8,15 +8,14 @@
std::vector<uint32_t > readFromFile(const char* filePath) std::vector<uint32_t > readFromFile(const char* filePath)
{ {
std::vector<uint32_t > numbers; std::vector<uint32_t > numbers;
std::cout << "read file: " << filePath << "..." << std::endl; //std::cout << "read file: " << filePath << "..." << std::endl;
std::ifstream in(filePath); std::ifstream in(filePath);
if (in.is_open()) if (in.is_open())
{ {
std::string line; std::string line;
std::getline(in, line); std::getline(in, line);
int count = std::stoi(line); int count = std::stoi(line);
//std::cout << "total: " << count << " elems" << std::endl;
std::cout << "total: " << count << " elems" << std::endl;
int idx = 0; int idx = 0;
while (in.good() && idx < count) { while (in.good() && idx < count) {

View File

@ -11,85 +11,85 @@
// TODO: // TODO:
// - combine partition function // - combine partition function
// - wirth
// - fix Median of Medians // - fix Median of Medians
// - fix randomized select
// - use custom swap for just pointer swapping (check difference)
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
// read test values from input file // read test values from input file
Timing::getInstance()->startRecord("init"); std::string type = "init\t\t\t\t\t";
Timing::getInstance()->startRecord(type);
std::vector<uint32_t > numbers = readFromFile("testdata"); std::vector<uint32_t > numbers = readFromFile("testdata");
std::cout << "just read " << numbers.size() << " values" << std::endl; Timing::getInstance()->stopRecord(type);
Timing::getInstance()->stopRecord("init");
// index of median // index of median is exactly middle for odd datasets, not supported for even :p
if (numbers.size() % 2 == 0) if (numbers.size() % 2 == 0)
{ {
// TODO: just use the next element? calc arithmetic mean? // TODO: just use the next element? calc arithmetic mean?
std::cout << "TODO: define how to handle even datasets" << std::endl; std::cout << "TODO: define how to handle even datasets" << std::endl;
return 1; return 1;
} }
uint32_t idxMed = (numbers.size() - 1) / 2; uint32_t idxMed = (numbers.size() - 1) / 2;
std::cout << "idx median = " << idxMed << " of " << numbers.size() << std::endl; std::cout << "calculating median on index " << idxMed << " of " << numbers.size() << " elems..." << std::endl;
// vollst<73>ndige Sortierung mit Quicksort und Ausgabe des mittleren Elements // create an array from vector for algorithms using array params
Timing::getInstance()->startRecord("quicksort"); uint32_t* array = new uint32_t[numbers.size()];
std::cout << "quicksort median: " << getQuicksortMedian(numbers, idxMed) << std::endl;
Timing::getInstance()->stopRecord("quicksort");
// vollständige Sortierung mit Quicksort und Ausgabe des mittleren Elements
type = "quicksort median\t\t\t";
Timing::getInstance()->startRecord(type);
std::cout << type << getQuicksortMedian(numbers, idxMed) << std::endl;
Timing::getInstance()->stopRecord(type);
// comparison of std::qsort and std::sort
// using std quicksort for array // using std quicksort for array
// TODO: decide how the fuck to create a dynamic size array from vector... type = "array quicksort\t\t\t\t";
//size_t* array = new size_t[numbers.size]; // create and fill new array
//size_t* array = &numbers[0]; // just a pointer to first element as array
//size_t* array[numbers.size()]; // invalid c++ -> non-constant expression!
//std::copy(numbers.begin(), numbers.end(), array);
//size_t* tmp = numbers.data(); // c++11 returns pointer to first elem
uint32_t* array = new uint32_t[999999]; // create and fill new array
std::copy(numbers.begin(), numbers.end(), array); std::copy(numbers.begin(), numbers.end(), array);
Timing::getInstance()->startRecord("array quicksort"); Timing::getInstance()->startRecord(type);
std::qsort(array, numbers.size(), sizeof(uint32_t), compare); std::qsort(array, numbers.size(), sizeof(uint32_t), compare);
std::cout << "array quicksort median: " << array[idxMed] << std::endl; std::cout << type << array[idxMed] << std::endl;
Timing::getInstance()->stopRecord("array quicksort"); Timing::getInstance()->stopRecord(type);
// comparison "quick" sort with std::sort (introsort) // comparison "quick" sort with std::sort (introsort)
type = "array std sort\t\t\t\t";
std::copy(numbers.begin(), numbers.end(), array); std::copy(numbers.begin(), numbers.end(), array);
Timing::getInstance()->startRecord("array std sort"); Timing::getInstance()->startRecord(type);
std::sort(array, array + numbers.size()); std::sort(array, array + numbers.size());
std::cout << "array std sort median: " << array[idxMed] << std::endl; std::cout << type << array[idxMed] << std::endl;
Timing::getInstance()->stopRecord("array std sort"); Timing::getInstance()->stopRecord(type);
// vorgestellter Randomzized - Select rekursiv implementiert // vorgestellter Randomzized - Select rekursiv implementiert
Timing::getInstance()->startRecord("randomized select"); type = "randomized select\t\t\t";
std::cout << "randomized select: " << getRandomizedSelectMedian(numbers, 0, numbers.size() - 1, idxMed + 1) << std::endl; Timing::getInstance()->startRecord(type);
Timing::getInstance()->stopRecord("randomized select"); std::cout << type << getRandomizedSelectMedian(numbers, 0, numbers.size() - 1, idxMed + 1) << std::endl;
Timing::getInstance()->stopRecord(type);
// ein weiterer Median - Algorithmus aus der Literatur - implemented with std::vector // ein weiterer Median - Algorithmus aus der Literatur - implemented with std::vector
type = "vector median of medians\t\t";
std::vector<uint32_t> mom_numbers = std::vector<uint32_t>(numbers); std::vector<uint32_t> mom_numbers = std::vector<uint32_t>(numbers);
Timing::getInstance()->startRecord("vector median of medians"); Timing::getInstance()->startRecord(type);
std::cout << "vector median of medians: " << mom_numbers[findMedianOfMedians(mom_numbers, 0, numbers.size() - 1, idxMed + 1)] << std::endl; std::cout << type << mom_numbers[findMedianOfMedians(mom_numbers, 0, numbers.size() - 1, idxMed + 1)] << std::endl;
Timing::getInstance()->stopRecord("vector median of medians"); Timing::getInstance()->stopRecord(type);
// ein weiterer Median - Algorithmus aus der Literatur - realized with array // ein weiterer Median - Algorithmus aus der Literatur - realized with array
/*std::copy(numbers.begin(), numbers.end(), array); /*type = "array median of medians";
Timing::getInstance()->startRecord("array median of medians"); std::copy(numbers.begin(), numbers.end(), array);
std::cout << "array median of medians: " << getMedianOfMedians(array, 0, numbers.size() - 1, idxMed + 1) << std::endl; Timing::getInstance()->startRecord(type);
Timing::getInstance()->stopRecord("array median of medians");*/ std::cout << type << ": \t\t" << getMedianOfMedians(array, 0, numbers.size() - 1, idxMed + 1) << std::endl;
Timing::getInstance()->stopRecord(type);*/
// noch ein ein weiterer Median - Algorithmus weil wir so cool sind // noch ein ein weiterer Median - Algorithmus weil wir so cool sind
Timing::getInstance()->startRecord("wirth"); type = "wirth kth element\t\t\t";
std::cout << "wirth kth element: " << getWirthKthSmallest(numbers, idxMed) << std::endl; Timing::getInstance()->startRecord(type);
Timing::getInstance()->stopRecord("wirth"); std::cout << type << getWirthKthSmallest(numbers, idxMed) << std::endl;
Timing::getInstance()->stopRecord(type);
// Verwendung des C++ STL function templates nth_element // Verwendung des C++ STL function templates nth_element
Timing::getInstance()->startRecord("nth element"); type = "nth element\t\t\t\t";
Timing::getInstance()->startRecord(type);
std::nth_element(numbers.begin(), numbers.begin() + idxMed, numbers.end()); std::nth_element(numbers.begin(), numbers.begin() + idxMed, numbers.end());
std::cout << "nth element: " << numbers[idxMed] << std::endl; std::cout << type << numbers[idxMed] << std::endl;
Timing::getInstance()->stopRecord("nth element"); Timing::getInstance()->stopRecord(type);
//Timing::getInstance()->getResults();
Timing::getInstance()->print(); Timing::getInstance()->print();
return 0; return 0;
} }