My outpur is inaccurate

#include
using namespace std;

void merge(int *a, int s, int e){
int med = (s+e)/2;
int i = s;
int j = med+1;
int k = s;

int temp[100];

while(i<=med && j<=e){
	if(a[i] < a[j]){
		temp[k++] = a[i++];
	}
	else{
		temp[k++] = a[j++];
	}
}

while(i<med){
	temp[k++] = a[i++];
}
while(j<e){
	temp[k++] = a[j++];
}

for(int i = s; i<=e; i++){
	a[i] = temp[i];
}

}

void mergesort(int a[], int s, int e){
if(s>=e){
return;
}
//three steps
//1. Divide
int med = (s+e)/2;

//recursively the arrays
mergesort(a, s, med);
mergesort(a, med+1, e);

//merge

merge(a, s, e);

}

int main(){

int a[100];
int n;
cin>>n;

for(int i=0; i<n; i++){
	cin>>a[i];
}

mergesort(a, 0, n-1);

for(int i = 0; i<n; i++){
	cout<<a[i]<<" , ";
}

return 0;

}

Hey @sarthaks03
i have debugged your code and i found that in your void merge function you are calculating mid point again moreover you are storing value of an array in single temp variable for that I have edited your code here : https://ide.codingblocks.com/s/328458 with comments to understand.
Hope it will help you.

but you used two temporary arrays instead of one as shown in the video.

Yes @sarthaks03 :grinning:you can use one temp array also , make L[ ] & R[ ] act as one , but to avoid complexity of the program and to give a better explanation I took 2 arrays .
Rest you can do it using single temp array also :grinning:
Hope this was helpful to you.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.