I have written this mergesort code but the output values are very weird. I have found out that the problem lies in code for mergee().
#include <iostream>
using namespace std;
void mergee(int arr[], int l, int mid, int r){
int n1=(mid-l+1);
int n2=(r-mid);
int a[n1];
int b[n2];
for(int i=0;i<n1;i++){
a[i]=arr[l+i];
}
for(int i=0;i<n2;i++){
b[i]=arr[mid+1+i];
}
int i=0;
int j=0;
int k=1;
while(i<n1 && j<n2){
if(a[i]<b[j]){
arr[k]=a[i];
k++;
i++;
}
else{
arr[k]=b[j];
k++;
j++;
}
}
while(i<n1){
arr[k]=a[i];
k++;
i++;
}
while(j<n2){
arr[k]=b[j];
k++;
j++;
}
}
output: Enter number of elements: 5 Enter unsorted elements: 3 7 5 9 2 Sorted elements are: 3 3 2 7 2
I have no idea what is wrong. a couple of values are been outputed twice. been trying to debug for a hilw but to no avail.