Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I am trying to implement merge sort algorthim by using c++ void merge(int array[], int p, int q, int r) { int n1 = q
I am trying to implement merge sort algorthim by using c++
void merge(int array[], int p, int q, int r) { int n1 = q - p + 1; int n2 = r - q; int *left = new int[n1 + 1]; int *right = new int[n2 + 1]; for(int i = 0; i < n1; i++) left[i] = array[p + i]; for(int j = 0; j < n2; j++) right[j] = array[q + j+1]; left[n1+1] = INT_MAX; right[n2+1] = INT_MAX;
int i = 0, j = 0; for(int k = 0; k < r; ++k) { if(left[i] <= right[j]) { array[k] = left[i]; i++; } else if(right[j] < left[i]) { array[k] = right[j]; j++; } } delete left; left =0; delete right; right =0; }
void mergesort(int array[], int p, int r) { if(p < r) { int q = (p + r) / 2; mergesort(array, p, q - 1); mergesort(array, q, r); merge(array, p, q, r); } }
Actuall, I cannot do the delete statment but I need to delete left and right array after using
an you help me?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started