Add previous solution for selection sort.

This commit is contained in:
Jan Paulus 2026-06-18 09:24:12 +02:00
parent cbe48ba34f
commit 67ae1ba1ca
2 changed files with 26 additions and 0 deletions

View File

@ -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]);
}
}

View File

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