Add solution for bubblesort and qsort.
This commit is contained in:
parent
556c08db20
commit
3ed0108d81
@ -31,3 +31,21 @@ void selectionsort(int array[], unsigned int len)
|
|||||||
tausche(&array[maxIdx], &array[unsortierteLaenge-1]);
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -46,7 +46,8 @@ int main()
|
|||||||
zahlen[i] = gibZahlEin();
|
zahlen[i] = gibZahlEin();
|
||||||
}
|
}
|
||||||
|
|
||||||
selectionsort(zahlen, anzahl);
|
//selectionsort(zahlen, anzahl);
|
||||||
|
bubblesort(zahlen, anzahl);
|
||||||
|
|
||||||
printf("Sortierte Zahlen:\n");
|
printf("Sortierte Zahlen:\n");
|
||||||
for(int i = 0; i < anzahl; i++)
|
for(int i = 0; i < anzahl; i++)
|
||||||
|
|||||||
@ -31,6 +31,11 @@ void gibArrayAus(int array[], int anzahl)
|
|||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int vergleiche(const void *arg1, const void *arg2)
|
||||||
|
{
|
||||||
|
return *(const int *)arg1 - *(const int *)arg2;
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int zufallszahlen[ANZAHL];
|
int zufallszahlen[ANZAHL];
|
||||||
@ -42,7 +47,9 @@ int main()
|
|||||||
gibArrayAus(zufallszahlen, ANZAHL);
|
gibArrayAus(zufallszahlen, ANZAHL);
|
||||||
|
|
||||||
starteUhr();
|
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());
|
printf("Zahlen sortiert nach %lf Sekunden.\n", messeZeitInSek());
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user