I am writing a merge sort function in C++ and it is giving me errors. Can you fi
ID: 3789098 • Letter: I
Question
I am writing a merge sort function in C++ and it is giving me errors. Can you fix my code? My function has to be of the form: void mergeSort(int* p, int length).
void mergeSort(int* p, int length)
{
//Make two halved sized arrays
int length1 = length / 2;
int length2 = length - (length / 2);
int* a = new int[length1];
int* b = new int[length2]; //The size of this array stores the extra number if length is odd
bool sorted = false;
int i = 0;
int j = 0;
int k = 0;
//Store the first half of 'array p' to 'array a'
for (int i = 0; i < length1; i++)
{
*(a + i) = *(p + i);
}
//Store the second half of 'array p' to 'array b'
for (int i = length1; i < length2; i++)
{
*(b + i) = *(p + i);
}
if (length1 > 1)
{
mergeSort(a, length1);
mergeSort(b, length2);
}
while(!sorted)
{
if (*(a + i) == NULL)
{
while (*(b + j) != NULL)
{
*(p + k) = *(b + j);
j++;
k++;
}
sorted = true;
}
else if (*(b + j) == NULL)
{
while (*(a + i) != NULL)
{
*(p + k) = *(a + i);
i++;
k++;
}
sorted = true;
}
else if (*(a + i) <= *(b + j))
{
*(p + k) = *(a + i);
i++;
k++;
}
else if (*(a + i) > *(b + j))
{
*(p + k) = *(b+ j);
j++;
k++;
}
}
//Deallocate memory
delete a;
delete b;
a = NULL;
b = NULL;
}
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int a[30], i, b[30],length;
cout<<"enter length";
cin>>length;
cout<<"enter the elements ";
for (i = 0; i < length; i++)
{
cin>>a[i];
}
mergeSort(a, length);
cout<<"sorted array ";
for (i = 0; i < length; i++)
{
cout<<a[i];
}
cout<<"enter the elements ";
for (i = 0; i < length; i++)
{
cin>>b[i];
}
mergeSort(b, length);
cout<<"sorted array ";
for (i = 0; i < 5; i++)
{
cout<<b[i];
}
getch();
}
void mergeSort(int* p, int length)
{
//Make two halved sized arrays
int length1 = length / 2;
int length2 = length - (length / 2);
int* a = new int[length1];
int* b = new int[length2]; //The size of this array stores the extra number if length is odd
bool sorted = false;
int i = 0;
int j = 0;
int k = 0;
//Store the first half of 'array p' to 'array a'
for (int i = 0; i < length1; i++)
{
*(a + i) = *(p + i);
}
//Store the second half of 'array p' to 'array b'
for (int i = length1; i < length2; i++)
{
*(b + i) = *(p + i);
}
if (length1 > 1)
{
mergeSort(a, length1);
mergeSort(b, length2);
}
while(!sorted)
{
if (*(a + i) == 0)
{
while (*(b + j) != 0)
{
*(p + k) = *(b + j);
j++;
k++;
}
sorted = true;
}
else if (*(b + j) == 0)
{
while (*(a + i) != 0)
{
*(p + k) = *(a + i);
i++;
k++;
}
sorted = true;
}
else if (*(a + i) <= *(b + j))
{
*(p + k) = *(a + i);
i++;
k++;
}
else if (*(a + i) > *(b + j))
{
*(p + k) = *(b+ j);
j++;
k++;
}
}
//Deallocate memory
delete a;
delete b;
a = NULL;
b = NULL;
}
This code is working fine. We can use NULL with the arithematic operators. Here when the user specifies the lenght ie the lenght of the array the mergeSort function can be applied and sorting takes place accordingly on the given array.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.