From 16f10c0937718f331c2319d8acf15569f0195634 Mon Sep 17 00:00:00 2001 From: Karl Date: Sun, 18 Oct 2020 16:41:52 +0200 Subject: [PATCH] Separate approx. MoM and exact with quickselect --- main.cpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/main.cpp b/main.cpp index b62165e..8997255 100644 --- a/main.cpp +++ b/main.cpp @@ -13,7 +13,7 @@ int main(int argc, char** argv) { // read test values from input file - std::string type = "init\t\t\t\t\t"; + std::string type = "init\t\t\t\t\t\t"; Timing::getInstance()->startRecord(type); std::vector numbers = readFromFile("testdata"); Timing::getInstance()->stopRecord(type); @@ -32,14 +32,14 @@ int main(int argc, char** argv) uint32_t* array = new uint32_t[numbers.size()]; // vollständige Sortierung mit Quicksort und Ausgabe des mittleren Elements - type = "quicksort median\t\t\t"; + type = "quicksort median\t\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 - type = "array quicksort\t\t\t\t"; + type = "array quicksort\t\t\t\t\t"; std::copy(numbers.begin(), numbers.end(), array); Timing::getInstance()->startRecord(type); std::qsort(array, numbers.size(), sizeof(uint32_t), compare); @@ -47,7 +47,7 @@ int main(int argc, char** argv) Timing::getInstance()->stopRecord(type); // comparison "quick" sort with std::sort (introsort) - type = "array std sort\t\t\t\t"; + type = "array std sort\t\t\t\t\t"; std::copy(numbers.begin(), numbers.end(), array); Timing::getInstance()->startRecord(type); std::sort(array, array + numbers.size()); @@ -55,25 +55,31 @@ int main(int argc, char** argv) Timing::getInstance()->stopRecord(type); // vorgestellter Randomzized - Select rekursiv implementiert - type = "randomized select\t\t\t"; + type = "randomized select\t\t\t\t"; Timing::getInstance()->startRecord(type); 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 - type = "vector median of medians\t\t"; + type = "vector median of medians only (approximative)\t"; + Timing::getInstance()->startRecord(type); + std::cout << type << numbers[pivot(numbers, 0, numbers.size() - 1)] << std::endl; + Timing::getInstance()->stopRecord(type); + + // ein weiterer Median - Algorithmus aus der Literatur - implemented with std::vector + type = "vector median of medians + quickselect (exact)\t"; Timing::getInstance()->startRecord(type); std::cout << type << getMedianOfMedians(numbers, 0, numbers.size() - 1, idxMed) << std::endl; Timing::getInstance()->stopRecord(type); // noch ein ein weiterer Median - Algorithmus weil wir so cool sind - type = "wirth kth element\t\t\t"; + type = "wirth kth element\t\t\t\t"; Timing::getInstance()->startRecord(type); std::cout << type << getWirthKthSmallest(numbers, idxMed) << std::endl; Timing::getInstance()->stopRecord(type); // Verwendung des C++ STL function templates nth_element - type = "nth element\t\t\t\t"; + type = "nth element\t\t\t\t\t"; Timing::getInstance()->startRecord(type); std::nth_element(numbers.begin(), numbers.begin() + idxMed, numbers.end()); std::cout << type << numbers[idxMed] << std::endl;