# MergeSort function not working with randomn number

• 02-11-2005
MergeSort function not working with randomn number
Hi,
I am still a programmer in the process of learning, I am trying to implement this program with generates 20 randomn numbers and uses the merge sort to organize them. My Function call doesn't seem to be working properly as I am able generate the numbers but not sort them properly. Thanks. The code is attached below.

Code:

```#include <iostream> #include <stdlib.h> using namespace std; void Merge( float A[], int F, int Mid, int L); void Mergesort(float A[], int F, int L); int main(int argc, char *argv[]) {   float x[20000];   int first, middle, last;   x[0] = 1;   for (int i = 0; i < 10; i++)   {     {   x[i+1]=0.5*(x[i]-(4/(x[i])));   if (x[i] <= 0)   {   cout << x[i] * -1 <<endl;   }   else {     cout << x[i] << endl;   }   }   }   Merge (x, first, middle, last);   Mergesort(x, first, last);   for (int i = 0; i <10; i++)   cout << x <<endl;          system("PAUSE");   return 0;   } void Merge( float A[], int F, int Mid, int L) { float x[20000]; int First1 = F; int Last1 = Mid; int First2 = Mid + 1; int Last2 = L; int Index = First1; for (; (First1 <= Last1) && (First2 <= Last2); ++Index) { if (A[First1] < A[First2]) { x[Index] = A[First1]; ++First1; } else { x[Index] = A[First2]; ++First2; } } for (;First1 <=Last1; ++First1, ++Index) x[Index] = A[First1]; for (;First2 <= Last2; ++First2, ++Index) x[Index] = A[First2]; for (Index = F; Index <= L; ++Index) A[Index] = x[Index]; system("PAUSE"); return; } void Mergesort(float A[], int F, int L) { int Mid = (F + L)/2; Mergesort(A,F,Mid); Mergesort(A,Mid+1,L); Merge(A,F,Mid,L); system("PAUSE"); return ; }```
• 02-11-2005
pianorain
You never assign values to first, last, or middle. That might cause some problems. ;)
• 02-11-2005
Hi thanks
I am still a bit confused, how do I assign values to first,last, and middle? Since I first generated some randomn numbers. Thank you sir.
• 02-11-2005
pianorain
Take a look between here:
Code:

```int main(int argc, char *argv[]) {   float x[20000];   int first, middle, last;   //snip   Merge (x, first, middle, last);```
And here. You never assign a value to first, middle, or last. You need to assign 0 to first, the last valid index to last, and some number in the middle to middle. Maybe:
Code:

```first = 0; middle = 4; last = 10;```
Or something like that.

By the way, I also get a Stack Overflow when trying to run. I'm almost positive that this is wrong:
Code:

```void Mergesort(float A[], int F, int L) { int Mid = (F + L)/2; Mergesort(A,F,Mid); Mergesort(A,Mid+1,L); Merge(A,F,Mid,L); system("PAUSE"); return ; }```
Consider the call to Mergesort with some array of floats, F = 0, L = 4. The next call is with F = 0, L = 2. The next call is with F = 0, L = 1. Every call after that will be with F = 0, L = 0, and it will do this until you run out of stack space. I'm pretty sure there's supposed to be some sort of condition in that Mergesort function.
• 02-11-2005
andyhunter
Why don't you look here .

The explaination is decent and you should see what you are doing wrong.

To pianorain, yes there needs to be a check in there. All recursive functions need to have a default check to break out of the recursion cycle otherwise they will all cause a stack overflow.

*edit*
Look at what I just found at cprogramming.com
• 02-12-2005
Salem