i am not able to find mistake in the given code why my merge sort function is not working and just returns the given array.
#include
using namespace std;
void merge(int *a, int s, int e)
{
int mid = (s + e) / 2;
int i = e;
int j = s;
int temp[100];
int k = s;
while (i <= mid && j <= e)
{
if (a[i] < a[j])
temp[k++] = a[i++];
else
{
temp[k++] = a[j++];
}
}
while (i <= mid)
temp[k++] = a[i++];
while (j <= e)
temp[k++] = a[j++];
for (int i = s; i < e; i++)
a[i] = temp[i];
return;
}
void mergesort(int *a, int s, int e)
{
if (s == e)
return;
int mid = (s + e) / 2;
mergesort(a, s, mid);
mergesort(a, mid + 1, e);
merge(a, s, e);
}
int main()
{
int a[] = {5, 4, 3, 1, 2};
int n = 5;
mergesort(a, 0, n-1);
for (int i = 0; i < n; i++)
cout << a[i];
cout << endl;
return 0;
}