Here's the code: (the algorithm is the usual one - comparing the first two elements in the arrays each time)

static void Merge(int[] Arr, int left, int mid, int right) { int[] TempArr = new int[right - left + 1]; int LeftPtr = left, LeftEnd = mid, RightPtr = mid + 1, RightEnd = right; int Index = 0; while (LeftPtr <= LeftEnd && RightPtr <= RightEnd) { if (Arr[LeftPtr] <= Arr[RightPtr]) TempArr[Index++] = Arr[LeftPtr++]; else TempArr[Index++] = Arr[RightPtr++]; } if (RightPtr > RightEnd) { while (LeftPtr <= LeftEnd) TempArr[Index++] = Arr[LeftPtr++]; } if (LeftPtr > LeftEnd) { while (RightPtr <= RightEnd) { TempArr[Index++] = Arr[RightPtr++]; [b]// This is the error line. It says index was out of range.[/b] } } for (int i = 0; i < TempArr.Length; ++i) { Arr[left++] = TempArr[i]; } }

Anyone has an idea where could my problem be?

Thanks for any help.