diff --git a/09/sortierung/sortierung.c b/09/sortierung/sortierung.c index b0e7e88..978d0d8 100644 --- a/09/sortierung/sortierung.c +++ b/09/sortierung/sortierung.c @@ -31,3 +31,21 @@ void selectionsort(int array[], unsigned int len) tausche(&array[maxIdx], &array[unsortierteLaenge-1]); } } + +void bubblesort(int array[], unsigned int anzahl) +{ + int wurdeGetauscht = 1; + + for(int i = anzahl-1; i >= 1 && wurdeGetauscht; i--) + { + wurdeGetauscht = 0; + for(int j = 0; j < i; j++) + { + if(array[j] > array[j+1]) + { + tausche(&array[j], &array[j+1]); + wurdeGetauscht = 1; + } + } + } +} \ No newline at end of file diff --git a/09/sortierung/sortierungTest.c b/09/sortierung/sortierungTest.c index 6ac0ce8..c78ca21 100644 --- a/09/sortierung/sortierungTest.c +++ b/09/sortierung/sortierungTest.c @@ -46,7 +46,8 @@ int main() zahlen[i] = gibZahlEin(); } - selectionsort(zahlen, anzahl); + //selectionsort(zahlen, anzahl); + bubblesort(zahlen, anzahl); printf("Sortierte Zahlen:\n"); for(int i = 0; i < anzahl; i++) diff --git a/09/sortierung/sortierungsvergleich.c b/09/sortierung/sortierungsvergleich.c index 1f7468c..e650703 100644 --- a/09/sortierung/sortierungsvergleich.c +++ b/09/sortierung/sortierungsvergleich.c @@ -31,6 +31,11 @@ void gibArrayAus(int array[], int anzahl) printf("\n"); } +int vergleiche(const void *arg1, const void *arg2) +{ + return *(const int *)arg1 - *(const int *)arg2; +} + int main() { int zufallszahlen[ANZAHL]; @@ -42,7 +47,9 @@ int main() gibArrayAus(zufallszahlen, ANZAHL); starteUhr(); - selectionsort(zufallszahlen, ANZAHL); + //selectionsort(zufallszahlen, ANZAHL); + //bubblesort(zufallszahlen, ANZAHL); + qsort(zufallszahlen, ANZAHL, sizeof(zufallszahlen[0]), vergleiche); printf("Zahlen sortiert nach %lf Sekunden.\n", messeZeitInSek());