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