megesort()

Hi
What's wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void fnMerge(long a[], int low, int high, int mid);

void msort(long int a[], int m, int n){
     if( (n-m)==1)
     return;
     else{
     msort(a, 0, n/2);
    msort(a, n/2 +1, n);
    fnMerge(a, m, n, n/2); 
}
...
void fnMerge(long a[], int low, int high, int mid)
{
     int i=0, j=0, k=0;
     long c[sizeof(a)];
       
     while(i<=mid  && j<high  )
     {
        if(a[i]<=a[j])
           c[k++]=a[i++];
        else{
              c[k++]=a[j++];
              }
     }
}    
     

Last edited on
What makes you think something is wrong? If you have a problem, then tell us what the problem is.
ُThe problem is that the screen just flashes. I am sure something is wrong.
The program works as expected when I make the call to msort a comment.
Last edited on
closed account (48T7M4Gy)
the screen just flashes

Could be the monitor is on its last legs or a loose power cable.

Either those or put a line with system ("pause") in a strategic spot in the program. or even getchar() has been known to stop flashing even in the case of an otherwise infinite loop.
The screen flashes because your program runs too quickly for you to read.

1) You click 'run'.
2) Windows creates a console window for your program.
3) Your program runs, writing stuff to the console window.
4) Your program ends.
5) Windows notices and gets rid of the console window.
6) You saw just a flash, if anything.

There are two ways to fix this.

1) Add a couple of lines like the following to the end of your program:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <limits>

...

int main()
{
  ...

  cout << "Press ENTER to quit.\n";
  cin.ignore( numeric_limits<streamsize>::max(), '\n' );
  return 0;
}


2) Run your program from the command prompt.


Hope this helps.
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/general/177366/
No, my main() has a while(1); statement before return 0; I am not too brand new
Have you tried to debug the program to see what happens? You probably have an error in your algorithm.
Taking another look at your code, I notice that you are allocating a four (or eight) byte VLA in your merge function, then iterating past its bounds...

Don't do that.


For merge sort, you should allocate all the memory you need first, via a standard container (like std::vector) or via new[] or malloc() if you must, and then pass that along for use by your merge subroutine.
Topic archived. No new replies allowed.