Merge sort not working

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;

}

Hey @mahfoozhaque91
please share ur code by https://ide.codingblocks.com/

  • Paste ur code there
  • Ctrl+S and click save
  • Paste the url here

got it ,thank you so much

1 Like

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.