I've been reading the Introduction to Algorithms and tried to implement the pseudocode, but I can't seem to pinpoint what is causing IndexOutOfRangeException
.
Merge
The error happens at
left[i] < right[j]
static void Merge(int[] array, int start, int middle, int end)
{
i = 1;
j = 1;
for (k = start; k < end; k++)
{
if (left[i] < right[j])
{
array[k] = left[i];
i = i + 1; // -> i++;
}
else
{
array[k] = right[j];
j = j + 1; // -> j++;
}
}
}
Solved
I solved it by setting all
i
andj
variables to0
andk
tok = start - 1