合并排序问题

时间:2011-08-29 07:52:22

标签: c# winforms algorithm mergesort

我正在制作各种排序算法的菜单,现在我陷入了合并排序。 单击执行按钮后遇到错误。我在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");
    }

3 个答案:

答案 0 :(得分:2)

关于您的具体问题:a是一个包含索引0, 1, 2, 3, 4的5个元素的数组,因此a[4]是最后一个元素。您从m_sort(0, 10 - 1) = m_sort(0, 9);开始。在m_sort()计算

mid = (right + left) / 2 = (9 + 0) / 2 = 4

并致电

merge(left, mid + 1, right) = merge(0, 4 + 1, 9) = merge(0, 5, 9).

merge(int left, int mid, int right)进行评估:

if (a[left] <= a[mid])   i.e.     if (a[0] <= a[5]) 

因此您可以访问超出范围的a[5]

我认为您的合并排序可以大大简化。您可以查看Web上的许多资源或算法的教科书。

答案 1 :(得分:1)

这是一个简化合并的方法的示例,假设每个列表都使用具有最高值的元素0进行排序:

int c[] = new int[a.Length + b.Length];
int aPos = 0;
int bPos = 0;

for(int i = 0; i < c.Length; i++)
{
    if(a[APos] > b[bPos])
    {
        c[i] = a[Apos];
        if(aPos < aPos.Length - 1)
            aPos++;
    }
    else
    {
        c[i] = b[bPos];
        if(bPos < bPos.Length - 1)
            bPos++;
    }
}

答案 2 :(得分:0)

// array of integers to hold values
private int[] a = new int[100];
private int[] b = new int[100];

// number of elements in array
private int x;

// Merge Sort Algorithm
public void sortArray()
{
  m_sort( 0, x-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] )
    {
      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;
  }
}