Add solution for quicksort.
This commit is contained in:
parent
4c414f9b33
commit
b87d5af5cc
@ -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)
|
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)
|
static void mische(int links[], int rechts[], int anzahlLinks, int anzahlRechts)
|
||||||
|
|||||||
@ -51,8 +51,8 @@ int main()
|
|||||||
|
|
||||||
starteUhr();
|
starteUhr();
|
||||||
//selectionSort(zufallszahlen, ANZAHL);
|
//selectionSort(zufallszahlen, ANZAHL);
|
||||||
mergeSort(zufallszahlen, ANZAHL);
|
//mergeSort(zufallszahlen, ANZAHL);
|
||||||
//quickSort(zufallszahlen, ANZAHL);
|
quickSort(zufallszahlen, ANZAHL);
|
||||||
//qsort(zufallszahlen, ANZAHL, sizeof(zufallszahlen[0]), vergleiche);
|
//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