Add exercises 09.

This commit is contained in:
2026-06-18 09:03:11 +02:00
parent 21b9b70871
commit 487f103de4
16 changed files with 389 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
SORTIERUNGLIBFILES = sortierung.c
SORTIERUNGTESTLIBFILES = $(SORTIERUNGLIBFILES) ../utils/zahlenEingabe.c
SORTIERUNGSVERGLEICHLIBFILES = $(SORTIERUNGLIBFILES) ../utils/stoppuhr.c ../utils/zufallsarray.c
sortierungTest:
gcc -Wall -o sortierungTest sortierungTest.c $(SORTIERUNGTESTLIBFILES)
sortierungsvergleich:
gcc -Wall -o sortierungsvergleich sortierungsvergleich.c $(SORTIERUNGSVERGLEICHLIBFILES)
+8
View File
@@ -0,0 +1,8 @@
#include "sortierung.h"
void tausche(int *zahl1, int *zahl2)
{
int tmp = *zahl1;
*zahl1 = *zahl2;
*zahl2 = tmp;
}
+9
View File
@@ -0,0 +1,9 @@
#ifndef SORTIERUNG_H
#define SORTIERUNG_H
void tausche(int *zahl1, int *zahl2);
void selectionsort(int array[], int anzahl);
void insertionsort(int array[], int anzahl);
void bubblesort(int array[], int anzahl);
#endif
+57
View File
@@ -0,0 +1,57 @@
/*****************************************************************
* Verwenden Sie Ihre Lösung für Selection Sort, die Sie zur *
* Übung von eindimensionalen Arrays geschrieben haben. *
* Implementieren Sie nun die Sortierungsfunktion nur mit *
* Zeigern. Implementieren Sie Ihre Lösung in sortierung.h bzw. *
* sortierung.c *
* *
* Beispiel für Sortierung einer Liste 5 3 0 2: *
* Setze Minimum der Restliste an 1. Position: 0 3 5 2 *
* Setze Minimum der Restliste an 2. Position: 0 2 5 3 *
* Setze Minimum der Restliste an 3. Position: 0 2 3 5 *
* *
* Beispielhafte Ausgabe: *
* >> Wie viele Zahlen (max. 100)? fsdf *
* >> Fehler! Erneute Eingabe: -1 *
* >> Fehler! Erneute Eingabe: 101 *
* >> Fehler! Erneute Eingabe: 5 *
* >> Geben Sie die 1. Zahl ein: dsf *
* >> Fehler! Erneute Eingabe: 45 *
* >> Geben Sie die 2. Zahl ein: 1 *
* >> Geben Sie die 3. Zahl ein: 0 *
* >> Geben Sie die 4. Zahl ein: -4 *
* >> Geben Sie die 5. Zahl ein: 1423 *
* Sortierte Zahlen: -4 0 1 45 1423 *
* *
*****************************************************************/
#include <stdio.h>
#include "../utils/zahlenEingabe.h"
#include "sortierung.h"
#define MAX_ZAHLEN 100
int main()
{
int zahlen[MAX_ZAHLEN];
int anzahl;
printf("Wie viele Zahlen (max. %d)? ", MAX_ZAHLEN);
anzahl = gibMinMaxZahlEin(0, MAX_ZAHLEN);
for(int i = 0; i < anzahl; i++)
{
printf("Geben Sie die %d. Zahl ein: ", i+1);
zahlen[i] = gibZahlEin();
}
selectionsort(zahlen, anzahl);
printf("Sortierte Zahlen:\n");
for(int i = 0; i < anzahl; i++)
printf("%3d ", zahlen[i]);
printf("\n");
return 0;
}
+53
View File
@@ -0,0 +1,53 @@
/************************************************************************************************
* Implementieren Sie die folgenden Sortieralgorithmen und vergleichen Sie deren Laufzeit mit
* untenstehendem Programm:
* 1. Nutzen Sie zunächst Ihre Lösung für SelectionSort.
* 2. Erweitern Sie nun sortierung.h bzw. sortierung.c um InsertionSort.
* 3. Erweitern Sie nun sortierung.h bzw. sortierung.c um BubbleSort.
* 4. Nutzen Sie abschließend die von der C-Standardbibliothek bereitgestellte qsort-Funktion.
*
* Hinweis: Sie können das Programm sortierungTest.c nutzen, um Ihre Implementierungen zu testen.
*
* Welche Laufzeit-Unterschiede ergeben sich?
*************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "sortierung.h"
#include "../utils/zufallsarray.h"
#include "../utils/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);
printf("Zahlen sortiert nach %lf Sekunden.\n", messeZeitInSek());
if(ANZAHL <= ANZEIGE_MAX)
gibArrayAus(zufallszahlen, ANZAHL);
return 0;
}