I want to create an insertionsort but I can’t get any further.
I solved it on my way until I get out of bounds, in the second "for-command" which I can’t solve. I’m not sure if I am on the right way, but I want to keep the solution simple.
int arrayzähler = 0;
int[] Speicherarray = new int[randomarray.Length];
//ausgabearray[1] = randomarray[1]; //für vergleich
foreach (int wert in randomarray)
{
if (wert == randomarray[0])
{
Speicherarray[0] = wert;
ausgabearray[0] = wert;
arrayzähler++;
continue; // erster wert = ausgabearray[0]
}
arrayzähler++;
for (int i = arrayzähler - 1; i >= arrayzähler - 1; i--)
{
for (int a = arrayzähler - 2; a >= arrayzähler - 2; a--)
{
if (Speicherarray[i] < Speicherarray[a])
{
Speicherarray[a] = Speicherarray[a + 1];
}
else if (Speicherarray[i] >= Speicherarray[a])
{
Speicherarray[a] = wert;
ausgabearray[i] = Speicherarray[i];
}
}
}
}
This is exact coding for insertion sort,