Add solution for bubblesort and qsort.

This commit is contained in:
paulusja 2026-06-18 12:51:33 +02:00
parent 556c08db20
commit 3ed0108d81
3 changed files with 28 additions and 2 deletions

View File

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

View File

@ -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++)

View File

@ -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());