diff --git a/Uebung 1/bubbleSort_commented.cpp b/Uebung 1/bubbleSort_commented.cpp new file mode 100644 index 0000000..da09058 --- /dev/null +++ b/Uebung 1/bubbleSort_commented.cpp @@ -0,0 +1,22 @@ +#include "bubbleSort.h" + +void bubbleSort(int *array, int array_size, bool asc) { + bool swapped = false; + do { + swapped = false; + // Dieser Loop läuft n mal und trägt O(n) zur Komplexität bei + for (int i = 0; i < array_size - 1; i++) { + if (array[i] > array[i + 1]) { + int temp = array[i]; + array[i] = array[i + 1]; + array[i + 1] = temp; + swapped = true; + } + } + // Dies ist der zweite Loop der im worst case ebenfalls O(n) mal läuft (wenn + // in jedem for loop mindest ein swap passiert) + } while (swapped == true); + + // Insgesamt bedeutet das, dass dieser Algorithmus eine Laufzeitkomplexität + // von O(n^2) hat. +} \ No newline at end of file