### merge sort problem

Hi,
I'm programming a mergesort algorithm, there is a problem which i cant figure it out,
Here is my code:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980`` ``````#include "stdafx.h" #include #include #include #include "myfile.h" using namespace std; myfile f; /* merge is the auxilery function of mergsort */ int merge(int a[],int p, int q, int r){ f.print("\nmerge:"); f.print(a,p,r); int n1=(q-p); int n2=(r-q); int * al=new int[n1]; int * ar=new int[n2]; for(int i=0;i>l; return l; } /* gettarray gets an array and pass it to be sorted */ int getarray(){ int l=getarraylength(); int * a = new int[l]; cout<<"\nPlease enter array numbers ::: "; int i=0; for(i=0;i>a[i]; } f.print(a,l); mergesort(a,0,l-1); return 0; } int _tmain(int argc, _TCHAR* argv[]) { cout<<"Main...\n"; getarray(); myfile f; /* hold the screen */ cout<<"\nEnter any key to continue ::: ";char ch;cin>>ch; } ``````

the array which it returns as a resault is filled with ' -33686019 '
i don't understand why?
How can i fix it?
1. You need to look at your merge ranges.
2. Why is that q global? It breaks the recursion.

Run this and look at the output. It's your code that just shows the parameters to the merge functions.

What should the output be and how does it differ from what your program produces?
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374`` ``````#include #include using namespace std; /* merge is the auxilery function of mergsort */ int merge(int a[],int p, int q, int r) { cout << "merge(p=" << p << ", q=" << q << ", r=" << r << ")" << endl; int n1=(q-p); int n2=(r-q); int * al=new int[n1]; int * ar=new int[n2]; for(int i=0;i>l; return l; } /* gettarray gets an array and pass it to be sorted */ int getarray() { int l=getarraylength(); int * a = new int[l]; cout<<"\nPlease enter array numbers ::: "; int i=0; for(i=0;i>a[i]; } mergesort(a,0,l-1); return 0; } int main(int argc, char* argv[]) { cout<<"Main...\n"; getarray(); /* hold the screen */ cout<<"\nEnter any key to continue ::: ";char ch;cin>>ch; }``````
Last edited on
Topic archived. No new replies allowed.