0

Please can anyone tell me why this merge sort code is not working.

#include <iostream> using namespace std; // merge sort void ptrarr(int *A, int n) { for (int i = 0; i < n; i++) { cout << A[i] << " "; } cout<<endl; } void merge(int A[], int mid, int lo, int hi) { int i, j, k, B[100]; i=lo, j=mid+1, k=lo; while (i <= mid && j <= hi) { if (A[i] < A[j]) { B[k] = A[i]; k++; i++; } else { B[k] = A[j]; k++; j++; } } while (i<=mid){ B[k]=A[i]; k++; i++; } while (j<=hi){ B[k]=A[j]; k++; j++; } for(int i=lo;i<=hi;i++){ A[i]=B[i]; } } void mergesort(int A[],int lo, int hi){ int mid; if(lo<hi){ mid=(lo+hi)/2; mergesort(A,lo,mid); mergesort(A,mid+1,hi); merge (A,mid,lo,hi); } } int main() { int A[] = {1,3,5,1,9,2}; int hi=sizeof(A)/sizeof(A[0]); ptrarr(A,hi); mergesort(A, 0, hi); for (int i = 0; i < 6; i++) { cout << A[i] << " "; } cout<<endl; return 0; https://code.sololearn.com/cn5DVn5NezMC/?ref=app

23rd Aug 2022, 1:41 PM
SHĆȘBHÃM
SHĆȘBHÃM - avatar
1 Answer
+ 4
You get out of range an array index. It will be needed to analize here with print statements or in another editor with debuging functionality.
23rd Aug 2022, 2:07 PM
JaScript
JaScript - avatar