Reputation: 41
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");
}
Upvotes: 1
Views: 432
Reputation: 1
// 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;
}
}
Upvotes: 0
Reputation:
Here is an example of a way to simplify the merge, assuming each list is sorted with element 0 having the highest value:
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++;
}
}
Upvotes: 1
Reputation: 9282
Regarding your concrete question: a
is an array of 5 elements with the indexes 0, 1, 2, 3, 4
, so a[4]
is the last element. You start with m_sort(0, 10 - 1) = m_sort(0, 9);
. In m_sort()
you compute
mid = (right + left) / 2 = (9 + 0) / 2 = 4
and call
merge(left, mid + 1, right) = merge(0, 4 + 1, 9) = merge(0, 5, 9).
In merge(int left, int mid, int right)
you evaluate:
if (a[left] <= a[mid]) i.e. if (a[0] <= a[5])
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.
Upvotes: 1