Algorithmen_Datenstrukturen/Uebung 1/bubbleSort_commented.cpp

22 lines
701 B
C++

#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.
}