归并排序问题
我正在制作各种排序算法的菜单,现在我陷入了合并排序。 我在单击执行按钮后遇到错误。我在 TextBox1 中输入了 5 个数字,在 TextBox2 中输入了另一组 5 个数字。它说索引超出了数组的范围。我在代码上指出了它出现的位置。有什么想法有什么问题吗?
private void ExeButton_Click(object sender, EventArgs e)
{
string[] numsInString = EntNum.Text.Split(' '); //split values in textbox
string[] numsInString1 = EntNum1.Text.Split(' ');
for (int j = 0; j < numsInString.Length; j++)
{
a[j] = int.Parse(numsInString[j]);
}
for (int j = 0; j < numsInString1.Length; j++)
{
b[j] = int.Parse(numsInString1[j]);
}
{
sortArray();
Display();
}
}
public void sortArray()
{
m_sort(0, 10 - 1);
}
public void m_sort(int left, int right)
{
int mid;
if (right > left)
{
mid = (right + left) / 2;
m_sort(left, mid);
m_sort(mid + 1, right);
merge(left, mid + 1, right);
}
}
public void merge(int left, int mid, int right)
{
int i, left_end, num_elements, tmp_pos;
left_end = mid - 1;
tmp_pos = left;
num_elements = right - left + 1;
while ((left <= left_end) && (mid <= right))
{
if (a[left] <= a[mid]) //index was outside the bounds of the the array
{
b[tmp_pos] = a[left];
tmp_pos = tmp_pos + 1;
left = left + 1;
}
else
{
b[tmp_pos] = a[mid];
tmp_pos = tmp_pos + 1;
mid = mid + 1;
}
}
while (left <= left_end)
{
b[tmp_pos] = a[left];
left = left + 1;
tmp_pos = tmp_pos + 1;
}
while (mid <= right)
{
b[tmp_pos] = a[mid];
mid = mid + 1;
tmp_pos = tmp_pos + 1;
}
for (i = 0; i < num_elements; i++)
{
a[right] = b[right];
right = right - 1;
}
}
private void ClearButton_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
}
public void Display()
{
int i;
String numbers = "";
for (i = 0; i < 10; i++)
numbers += a[i].ToString() + " ";
numbers += b[i].ToString() + " ";
richTextBox1.AppendText(numbers + "\n");
}
I'm making a menu of all kinds of sorting algorithm and now I'm stuck at merge sort.
I encountered an error after clicking the execute button. I entered 5 numbers in TextBox1 and another set of 5 numbers in TextBox2. It says that index was outside the bounds of the array. I indicated on the codes where it appeared. Any ideas what is the problem?
private void ExeButton_Click(object sender, EventArgs e)
{
string[] numsInString = EntNum.Text.Split(' '); //split values in textbox
string[] numsInString1 = EntNum1.Text.Split(' ');
for (int j = 0; j < numsInString.Length; j++)
{
a[j] = int.Parse(numsInString[j]);
}
for (int j = 0; j < numsInString1.Length; j++)
{
b[j] = int.Parse(numsInString1[j]);
}
{
sortArray();
Display();
}
}
public void sortArray()
{
m_sort(0, 10 - 1);
}
public void m_sort(int left, int right)
{
int mid;
if (right > left)
{
mid = (right + left) / 2;
m_sort(left, mid);
m_sort(mid + 1, right);
merge(left, mid + 1, right);
}
}
public void merge(int left, int mid, int right)
{
int i, left_end, num_elements, tmp_pos;
left_end = mid - 1;
tmp_pos = left;
num_elements = right - left + 1;
while ((left <= left_end) && (mid <= right))
{
if (a[left] <= a[mid]) //index was outside the bounds of the the array
{
b[tmp_pos] = a[left];
tmp_pos = tmp_pos + 1;
left = left + 1;
}
else
{
b[tmp_pos] = a[mid];
tmp_pos = tmp_pos + 1;
mid = mid + 1;
}
}
while (left <= left_end)
{
b[tmp_pos] = a[left];
left = left + 1;
tmp_pos = tmp_pos + 1;
}
while (mid <= right)
{
b[tmp_pos] = a[mid];
mid = mid + 1;
tmp_pos = tmp_pos + 1;
}
for (i = 0; i < num_elements; i++)
{
a[right] = b[right];
right = right - 1;
}
}
private void ClearButton_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
}
public void Display()
{
int i;
String numbers = "";
for (i = 0; i < 10; i++)
numbers += a[i].ToString() + " ";
numbers += b[i].ToString() + " ";
richTextBox1.AppendText(numbers + "\n");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
关于您的具体问题:
a
是一个由5个元素组成的数组,索引为0,1,2,3,4
,所以a[4]
是最后一个元素。您可以从m_sort(0, 10 - 1) = m_sort(0, 9);
开始。在m_sort()
中,您计算并调用
在
merge(int left, int mid, int right)
中,您进行评估:因此您可以访问
a[5]
这是越界的。我认为你的合并排序可以大大简化。您可能会查看网络上或算法教科书中的许多资源。
Regarding your concrete question:
a
is an array of 5 elements with the indexes0, 1, 2, 3, 4
, soa[4]
is the last element. You start withm_sort(0, 10 - 1) = m_sort(0, 9);
. Inm_sort()
you computeand call
In
merge(int left, int mid, int right)
you evaluate:so you access
a[5]
which is out of bounds.I think your merge sort can be simplified considerably. You might look at the many resources on the Web or in a textbook on algorithms.
下面是简化合并方法的示例,假设每个列表都按具有最高值的元素 0 进行排序:
Here is an example of a way to simplify the merge, assuming each list is sorted with element 0 having the highest value: