Add solution for quicksort.

This commit is contained in:
paulusja 2025-12-19 09:50:27 +01:00
parent 4c414f9b33
commit b87d5af5cc
2 changed files with 37 additions and 3 deletions

View File

@ -22,9 +22,43 @@ void selectionSort(int array[], int anzahl)
}
}
static int *sortiereUmPivot(int array[], unsigned int anzahl)
{
int *linksZgr = array;
int *rechtsZgr = array + anzahl - 1;
int *pivot = rechtsZgr;
rechtsZgr--;
while(linksZgr < rechtsZgr)
{
while(linksZgr < rechtsZgr && *linksZgr <= *pivot)
linksZgr++;
while(linksZgr < rechtsZgr && *rechtsZgr > *pivot)
rechtsZgr--;
if(*linksZgr > *rechtsZgr)
tausche(linksZgr, rechtsZgr);
}
if(*linksZgr <= *pivot)
linksZgr = pivot;
else
tausche(linksZgr, pivot);
return linksZgr;
}
void quickSort(int array[], int anzahl)
{
if(anzahl > 1)
{
int *pivot = sortiereUmPivot(array, anzahl);
unsigned int anzahlLinks = pivot - array;
quickSort(array, anzahlLinks);
quickSort(pivot, anzahl - anzahlLinks);
}
}
static void mische(int links[], int rechts[], int anzahlLinks, int anzahlRechts)

View File

@ -51,8 +51,8 @@ int main()
starteUhr();
//selectionSort(zufallszahlen, ANZAHL);
mergeSort(zufallszahlen, ANZAHL);
//quickSort(zufallszahlen, ANZAHL);
//mergeSort(zufallszahlen, ANZAHL);
quickSort(zufallszahlen, ANZAHL);
//qsort(zufallszahlen, ANZAHL, sizeof(zufallszahlen[0]), vergleiche);
printf("Zahlen sortiert nach %lf Sekunden.\n", messeZeitInSek());