Question: (50) Implement Merge-Sort algorithm in mergeSort.cpp, where you are expected to implement two functions, merge() and mergeSort(). You are expected to call merge() within mergeSort(),
(50) Implement Merge-Sort algorithm in mergeSort.cpp, where you are expected to implement two functions, merge() and mergeSort(). You are expected to call merge() within mergeSort(), and you are not expected to declare/ implement other additional functions nor change the main() function.
//mergeSort.cpp:
#include
using namespace std;
// Merges two sorted subarrays of A[].
// First sorted subarray is A[l..m].
// Second sorted subarray is A[m+1..r].
// You might want to call this function in function mergeSort().
void merge(int A[], int l, int m, int r)
{
}
// using mergeSort to sort sub-array A[l..r]
// l is for left index and r is right index of the
// sub-array of A[] to be sorted
void mergeSort(int A[], int l, int r)
{
}
int main()
{
cout << "Please enter the length (number of elements) of the input array: ";
int n;
cin >> n;
if(n <= 0) {
cout << "Illegal input array length!" << endl;
return 0;
}
int* A = new int [n];
cout << "Please enter each element in the array" << endl;
cout << "(each element must be an integer within the range of int type)." << endl;
for(int i=0; i cout << "A[" << i << "] = "; cin >> A[i]; } cout << "Given array A[] is: "; for(int i=0; i cout << A[i] << ","; cout << A[n-1] << endl; mergeSort(A, 0, n-1); cout << "After quickSort, sorted array A[] is: "; for(int i=0; i cout << A[i] << ","; cout << A[n-1] << endl; delete [] A; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
