Zudem hat deine Lösung auch noch das Problem, dass es ungrade Zahlen abschneidet. Wenn 7 übergeben wird, werden nur 6 davon behandelt. Das ist wegen dem zweimal n/2 ergibt bei 7 zweimal 3 also 6.
Und so geht es wirklich:
void Merge(int* LeftArray, int* RightArray, int* ResultArray, int nLeft, int nRight)
{
int nL = 0, nR = 0, nResult = 0;
while(nL < nLeft && nR < nRight)
{
if(LeftArray[nL] > RightArray[nR])
{
ResultArray[nResult] = RightArray[nR];
nR++; nResult++;
}
else
{
ResultArray[nResult] = LeftArray[nL];
nL++; nResult++;
}
}
while(nL < nLeft)
{
ResultArray[nResult] = LeftArray[nL];
nL++; nResult++;
}
while(nR < nRight)
{
ResultArray[nResult] = RightArray[nR];
nR++; nResult++;
}
}
void MergeSort(int* Array, int nCount)
{
if (nCount > 1)
{
int nLeftCount = (nCount / 2);
int nRightCount = (nCount - nLeftCount);
int LeftArray[nLeftCount];
int RightArray[nRightCount];
for(int i = 0; i < nLeftCount; i++)
{ LeftArray[i] = Array[i]; }
for(int i = 0; i <nRightCount; i++)
{ RightArray[i] = pArray[i + nLeftCount]; }
MergeSort(LeftArray, nLeftCount);
MergeSort(RightArray, nRightCount);
Merge(LeftArray, RightArray, Array, nLeftCount, nRightCount);
}
}
Code ist nur hier reingeschrieben. Ich gebe keine Garantie, dass es keine Fehler drin hat. Aber in der Art sollte es eigentlich gehen.
Grüssli