diff --git a/10_sortierung/makefile b/10_sortierung/makefile new file mode 100644 index 0000000..f7df1a8 --- /dev/null +++ b/10_sortierung/makefile @@ -0,0 +1,7 @@ +SORTINGFILES = sortierung.c sortierungsvergleich.c stoppuhr.c zufallsarray.c + +sortierungsvergleich: + gcc -Wall -O2 -g -o sortierungsvergleich $(SORTINGFILES) + +clean: + rm -rf *.exe \ No newline at end of file diff --git a/10_sortierung/sortierung.c b/10_sortierung/sortierung.c new file mode 100644 index 0000000..d33597c --- /dev/null +++ b/10_sortierung/sortierung.c @@ -0,0 +1,32 @@ +#include "sortierung.h" + +static void tausche(int *zahl1, int *zahl2) +{ + int tmp = *zahl1; + *zahl1 = *zahl2; + *zahl2 = tmp; +} + +void selectionSort(int array[], int anzahl) +{ + for(int i = 0; i < anzahl - 1; i++) + { + int minIdx = i; + for(int j = i + 1; j < anzahl; j++) + { + if(array[j] < array[minIdx]) + minIdx = j; + } + tausche(&array[i], &array[minIdx]); + } +} + +void quickSort(int array[], int anzahl) +{ + +} + +void mergeSort(int array[], int anzahl) +{ + +} \ No newline at end of file diff --git a/10_sortierung/sortierung.h b/10_sortierung/sortierung.h new file mode 100644 index 0000000..b47c16a --- /dev/null +++ b/10_sortierung/sortierung.h @@ -0,0 +1,8 @@ +#ifndef SORTIERUNG_H +#define SORTIERUNG_H + +void selectionSort(int array[], int anzahl); +void quickSort(int array[], int anzahl); +void mergeSort(int array[], int anzahl); + +#endif \ No newline at end of file diff --git a/10_sortierung/sortierungsvergleich.c b/10_sortierung/sortierungsvergleich.c new file mode 100644 index 0000000..d43bf17 --- /dev/null +++ b/10_sortierung/sortierungsvergleich.c @@ -0,0 +1,58 @@ +/************************************************************************************************ + * Implementieren Sie die folgenden Sortieralgorithmen und vergleichen Sie deren Laufzeit mit + * untenstehendem Programm: + * 1. Nutzen Sie zunächst die bereitgestellte und aus Informatik 1 bekannte Lösung für SelectionSort. + * 2. Erweitern Sie nun sortierung.h bzw. sortierung.c um MergeSort. + * 3. Erweitern Sie nun sortierung.h bzw. sortierung.c um Quicksort. + * 4. Nutzen Sie abschließend die von der C-Standardbibliothek bereitgestellte qsort-Funktion. + * + * Hinweis: Das Array wird nur ausgegeben, wenn die Anzahl an Elementen noch klein genug für + * eine Darstellung ist. Standardmäßig wird eine sehr große Anzahl an Elementen sortiert, um + * eine spürbare Laufzeit zu erzwingen, und somit das Array nicht ausgegeben. Genaueres ist + * im Quellcode unten ersichtlich. + * + * Welche Laufzeit-Unterschiede ergeben sich? + *************************************************************************************************/ + +#include +#include +#include +#include "sortierung.h" +#include "zufallsarray.h" +#include "stoppuhr.h" + +#define ANZAHL_MAX 100000 +#define ANZAHL ANZAHL_MAX +#define ANZEIGE_MAX 100 + +void gibArrayAus(int array[], int anzahl) +{ + for(int i = 0; i < anzahl; i++) + { + printf(" %3d", array[i]); + } + printf("\n"); +} + + +int main() +{ + int zufallszahlen[ANZAHL]; + srand(0); + + fuelleArray(zufallszahlen, ANZAHL, -ANZAHL, ANZAHL); + + if(ANZAHL <= ANZEIGE_MAX) + gibArrayAus(zufallszahlen, ANZAHL); + + starteUhr(); + selectionSort(zufallszahlen, ANZAHL); + //mergeSort(zufallszahlen, ANZAHL); + + printf("Zahlen sortiert nach %lf Sekunden.\n", messeZeitInSek()); + + if(ANZAHL <= ANZEIGE_MAX) + gibArrayAus(zufallszahlen, ANZAHL); + + return 0; +} diff --git a/10_sortierung/stoppuhr.c b/10_sortierung/stoppuhr.c new file mode 100644 index 0000000..826af31 --- /dev/null +++ b/10_sortierung/stoppuhr.c @@ -0,0 +1,14 @@ +#include +#include "stoppuhr.h" + +static clock_t zeitStempel = 0; + +void starteUhr() +{ + zeitStempel = clock(); +} + +double messeZeitInSek() +{ + return (double)(clock() - zeitStempel) / CLOCKS_PER_SEC; +} \ No newline at end of file diff --git a/10_sortierung/stoppuhr.h b/10_sortierung/stoppuhr.h new file mode 100644 index 0000000..9def335 --- /dev/null +++ b/10_sortierung/stoppuhr.h @@ -0,0 +1,7 @@ +#ifndef STOPPUHR_H +#define STOPPUHR_H + +void starteUhr(); +double messeZeitInSek(); + +#endif \ No newline at end of file diff --git a/10_sortierung/zufallsarray.c b/10_sortierung/zufallsarray.c new file mode 100644 index 0000000..da16e01 --- /dev/null +++ b/10_sortierung/zufallsarray.c @@ -0,0 +1,8 @@ +#include +#include "zufallsarray.h" + +void fuelleArray(int array[], int anzahl, int min, int max) +{ + for(int i = 0; i < anzahl; i++) + array[i] = rand() % (max - min + 1) + min; +} \ No newline at end of file diff --git a/10_sortierung/zufallsarray.h b/10_sortierung/zufallsarray.h new file mode 100644 index 0000000..3e71819 --- /dev/null +++ b/10_sortierung/zufallsarray.h @@ -0,0 +1,6 @@ +#ifndef ZUFALLSARRAY_H +#define ZUFALLSARRAY_H + +void fuelleArray(int array[], int anzahl, int min, int max); + +#endif \ No newline at end of file