So I am working on merge sort in C# and I keep getting the IndexOutOfRangeException error on a piece of code. I am trying to debug and it looks like it has something to do with left[i]. This is a list of a seperate SortList Class.
The following code throws the error:
if (left.Count < mid)
{
for (int i = 0; i < mid; i++)
{
left[i] = list[i];
}
for (int i = mid; i < list.Count; i++)
{
right[i] = list[i];
}
}
The following error is thrown:
System.IndexOutOfRangeException
HResult=0x80131508
Message=De index ligt buiten de matrixgrenzen.
Source=2-Sorting
StackTrace:
at ALGA.SortList.set_Item(Int32 index, Int32 value) in C:\Users\Tristan\Documents\School\GitHub repositories\week-2-sorting-tommeren\2-Sorting\SortList.cs:line 84
at ALGA.Mergesort.mergesort(ISortList list) in C:\Users\Tristan\Documents\School\GitHub repositories\week-2-sorting-tommeren\2-Sorting\Mergesort.cs:line 26
at ALGA_test.MergesortTest.MergesortEfficiently() in C:\Users\Tristan\Documents\School\GitHub repositories\week-2-sorting-tommeren\2-Sorting-Test\MergesortTest.cs:line 53
Thank you in advance.
So the error is gone while initializing the SortList but the debug information doesn't give me any info on left or right, while I iterated through it multiple times. Do you know why?
User contributions licensed under CC BY-SA 3.0