0

I'm doing a lab where I must use three sorting methods, then merge sort the three arrays. While separating the larger array, I'm getting a

"run time check error #2 stack around the variable x was corrupted:

It does this for y and z as well. I think it is happening where I break up the bigarray into smaller ones, but not sure, and cannot figure out why it's happening. The code is below:

```
int main()
{
int bigarray[151];
int x[50];
int y[50];
int z[50];
srand ((unsigned) time(NULL));
for (int i=0; i<151 ;i++)
{
bigarray[i]=rand()%1000;
cout<< " "<<bigarray[i];
}
cout <<endl<<endl<<endl;
cout<<" 1 A...THIS IS THE FIRST UNSORTED 50 ELEMENTS OF THE ARRY "<<endl;
for(int i=0; i < 50; i++)// this is
{
x[i] = bigarray[i];
cout <<" "<<x[i];
}
cout<<endl<<endl<<endl<<endl<<"2 A...THIS IS THE SECOND 50 ELEMENTS OF THE ARRAY "<<endl<<endl;
for(int i=50; i < 101; i++)
{
y[i] =bigarray[i];
cout <<" "<<y[i];
}
cout<<endl<<endl<<endl<<endl<<" 3 A ... THIS IS THE THIRD 50 ELEMENTS OF THE ARRAY "<<endl;
for(int i=100; i < 151; i++)
{
z[i] =bigarray[i] ;
cout <<" "<<z[i];
}
insertion_sort(x);
selection_sort(y);
bubble_sort(z);
merge_sort(x, y, z);
return 0;
}
```

the cpp definitions for the header:

```
void insertion_sort(int x[])
{
int key,i;
for(int j=0;j<50;j++)
{
key=x[j];
i=j-1;
while(x[i]>key && i>=0)
{
x[i+1]=x[i];
i--;
}
x[i+1]=key;
}
}
void selection_sort(int y[])
{
int tmp;
int min;
for(int i=0;i<50;i++)
{
min = i;
for(int x=i; x<50; x++)
{
if(y[x] < y[min])
{
min = x;
}
}
tmp = y[i];
y[i] = y[min];
y[min] = tmp;
}
}
void bubble_sort(int z[])
{
int i,j;
for(i=0;i<50;i++)
{
for(j=0;j<i;j++)
{
if(z[i]>z[j])
{
int temp=z[i];
z[i]=z[j];
z[j]=temp;
}
}
}
}
void merge_sort(int x[], int y[], int z[])
{
int indexx = 0; // initialize the Array Indices
int indexy = 0;
int indexz = 0;
while((indexx < 50) && (indexy < 50))
{
if (x[indexx] < y[indexy])
{
z[indexz] = x[indexx];
indexx++; //increase the index
}
else
{
z[indexz] = y[indexy];
indexy++; //increase the index
}
indexz++; //move to the next position in the new array
}
// Push remaining elements to end of new array when 1 feeder array is empty
while (indexx < 50)
{
z[indexz] = x[indexx];
indexx++;
indexz++;
}
while (indexy< 50)
{
z[indexz] = y[indexy];
indexy++;
indexz++;
}
return;
}
```

The header:

```
#pragma once
void insertion_sort(int x[]);
void selection_sort(int y[]);
void bubble_sort(int z[]);
void merge_sort(int x[], int y[], int z[]);
```