From 647e14cb3c002823a3c75dc37c870bbf0bfb2d62 Mon Sep 17 00:00:00 2001 From: Samuel Oberhofer Date: Thu, 26 May 2022 10:01:23 +0200 Subject: [PATCH] Add quicksort --- Uebung 1/Algorithms.h | 27 +++++++++++++++++++++++++++ Uebung 1/Sortiment.cpp | 1 + 2 files changed, 28 insertions(+) diff --git a/Uebung 1/Algorithms.h b/Uebung 1/Algorithms.h index 31d7269..d4d4b0a 100644 --- a/Uebung 1/Algorithms.h +++ b/Uebung 1/Algorithms.h @@ -34,3 +34,30 @@ void insertionSort(Ware *waren[10], int sortby) { } } } + +void quicksort(Ware *waren[10], int l, int u) { + int i = l, j = u; + Ware *a; + int pivot = waren[(l + u) / 2]->getSeriennummer(); + while (i <= j) { + while (waren[i]->getSeriennummer() < pivot) { + i++; + } + while (waren[j]->getSeriennummer() > pivot) { + j--; + } + if (i <= j) { + a = waren[i]; + waren[i] = waren[j]; + waren[j] = a; + i++; + j--; + } + }; + if (l < j) { + quicksort(waren, l, j); + } + if (i < u) { + quicksort(waren, i, u); + } +} \ No newline at end of file diff --git a/Uebung 1/Sortiment.cpp b/Uebung 1/Sortiment.cpp index 0798fa7..9c097a1 100644 --- a/Uebung 1/Sortiment.cpp +++ b/Uebung 1/Sortiment.cpp @@ -14,6 +14,7 @@ void Sortiment::addWare(Ware *ware) { void Sortiment::sort(int modus) { switch (modus) { case 1: + quicksort(this->waren, 0, 9); break; case 2: bubbleSort(this->waren);