From 67ae1ba1caef89286a04cb8192ef6023f1a99501 Mon Sep 17 00:00:00 2001 From: paulusja Date: Thu, 18 Jun 2026 09:24:12 +0200 Subject: [PATCH] Add previous solution for selection sort. --- 09/sortierung/sortierung.c | 25 +++++++++++++++++++++++++ 09/sortierung/sortierung.h | 1 + 2 files changed, 26 insertions(+) diff --git a/09/sortierung/sortierung.c b/09/sortierung/sortierung.c index dbd15c0..b0e7e88 100644 --- a/09/sortierung/sortierung.c +++ b/09/sortierung/sortierung.c @@ -6,3 +6,28 @@ void tausche(int *zahl1, int *zahl2) *zahl1 = *zahl2; *zahl2 = tmp; } + +unsigned int findeMaxIdx(int array[], unsigned int len) +{ + unsigned int maxIdx = 0; + + for(int i = 1; i < len; i++) + { + if(array[i] > array[maxIdx]) + { + maxIdx = i; + } + } + + return maxIdx; +} + +void selectionsort(int array[], unsigned int len) +{ + for(int unsortierteLaenge = len; unsortierteLaenge >= 2; unsortierteLaenge--) + { + unsigned int maxIdx = findeMaxIdx(array, unsortierteLaenge); + + tausche(&array[maxIdx], &array[unsortierteLaenge-1]); + } +} diff --git a/09/sortierung/sortierung.h b/09/sortierung/sortierung.h index 398ebbf..1428830 100644 --- a/09/sortierung/sortierung.h +++ b/09/sortierung/sortierung.h @@ -5,5 +5,6 @@ void tausche(int *zahl1, int *zahl2); void selectionsort(int array[], unsigned int anzahl); void insertionsort(int array[], unsigned int anzahl); void bubblesort(int array[], unsigned int anzahl); +unsigned int findeMaxIdx(int array[], unsigned int len); #endif \ No newline at end of file