Initial checkin for sorting.

This commit is contained in:
paulusja 2025-12-11 13:08:08 +01:00
parent 5a4a806ed4
commit 9dd26cd3e2
8 changed files with 140 additions and 0 deletions

7
10_sortierung/makefile Normal file
View File

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

View File

@ -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)
{
}

View File

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

View File

@ -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 <stdio.h>
#include <stdlib.h>
#include <time.h>
#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;
}

14
10_sortierung/stoppuhr.c Normal file
View File

@ -0,0 +1,14 @@
#include <time.h>
#include "stoppuhr.h"
static clock_t zeitStempel = 0;
void starteUhr()
{
zeitStempel = clock();
}
double messeZeitInSek()
{
return (double)(clock() - zeitStempel) / CLOCKS_PER_SEC;
}

7
10_sortierung/stoppuhr.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef STOPPUHR_H
#define STOPPUHR_H
void starteUhr();
double messeZeitInSek();
#endif

View File

@ -0,0 +1,8 @@
#include <stdlib.h>
#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;
}

View File

@ -0,0 +1,6 @@
#ifndef ZUFALLSARRAY_H
#define ZUFALLSARRAY_H
void fuelleArray(int array[], int anzahl, int min, int max);
#endif