forked from paulusja/info1_uebungen
Add previous solution for selection sort.
This commit is contained in:
parent
cbe48ba34f
commit
67ae1ba1ca
@ -6,3 +6,28 @@ void tausche(int *zahl1, int *zahl2)
|
|||||||
*zahl1 = *zahl2;
|
*zahl1 = *zahl2;
|
||||||
*zahl2 = tmp;
|
*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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -5,5 +5,6 @@ void tausche(int *zahl1, int *zahl2);
|
|||||||
void selectionsort(int array[], unsigned int anzahl);
|
void selectionsort(int array[], unsigned int anzahl);
|
||||||
void insertionsort(int array[], unsigned int anzahl);
|
void insertionsort(int array[], unsigned int anzahl);
|
||||||
void bubblesort(int array[], unsigned int anzahl);
|
void bubblesort(int array[], unsigned int anzahl);
|
||||||
|
unsigned int findeMaxIdx(int array[], unsigned int len);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Loading…
x
Reference in New Issue
Block a user