Comment Bubblesort

This commit is contained in:
Samuel Oberhofer 2022-05-26 10:15:51 +02:00
parent 461b5f5535
commit ef0c63656d
1 changed files with 22 additions and 0 deletions

View File

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