这就是我的课程的设计方式:
class MergeSort
{
int[] mArray;
public MergeSort(int[] A)
{
mArray = A;
}
void Merge(int[] A,int p ,int q, int r)
{
int n1 = q - p + r;
int i = 0, j = 0;
int n2 = r - q;
int[] left = new int[n1+1];
int[] right = new int[n2+1];
for ( i = 0; i < n1; i++)
left[i] = A[p + i - 1];
for ( i = 0; i < n2; i++)
right[i] = A[q + i];
left[n1] = Int32.MaxValue;
right[n2] = Int32.MaxValue;
i = j = 0;
for (int k = p; k < r; k++)
{
if (left[i] <= right[j])
{
A[k] = left[i];
i++;
}
else
{
A[k] = right[j];
j++;
}
}
}
public void Merge_Sort(int[] A, int p, int r)
{
int q = 0;
mArray = A;
if(p<r)
{
q = (p+r)/2;
Merge_Sort(A,p,q);
Merge_Sort(A, q + 1, r);
Merge(A,p,q,r);
}
}
public string show()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mArray.Length; i++)
{
sb.Append(mArray[i].ToString() + " ");
}
return sb.ToString();
}
}
这就是我进行排序的方式:
int[] arr = { 10, 12, 5, 6, 30, 1, 11, 120, 12 };
MergeSort ms = new MergeSort(arr);
ms.Merge_Sort(arr, 0, arr.Length );
MessageBox.Show(ms.show());
但是我在我的功能Index was outside the bounds of the array.
中的许多地方不断收到错误。Merge
我试图实现 CLRS(Coremen) 书中所述的算法。但是我一直收到这个错误,我从 3 天开始就一直在这个错误 - 请帮忙。