merge sort runtime and logical errors

Hey there,
Can anyone kindly please help me or give some ideas on how to make my merge sort program work the way it should work, based on the functions I've declared before and implemented after the main program.

The problem is that the program compiled successfully, but when it comes to the sorting part after assigning random numbers on the array, it terminates abruptly. I was trying to call the RECURSIVEMERGESORT function, which I created based on the correct pseudo code that I've looked up on my previous books.

Moreover, I even tried to make a java equivalent of it, but it gave me a stack overflow error. What I'm also worried about is that despite correct implementations of the merge sort functions that I made, I would still get the undesired results. This happened when I managed to run the program and then set the array to any size less than 10 or 12; anything greater than that, then it would be a run time error.

I hope you consider helping me or just give some advice on how to make my program run well and with no bad results.

Below is the main program that I made. It's not the whole thing, as I excluded for a while the header file that includes the other 2 functions and a list adt declaration for a pointer-based merge sort.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <stdlib.h>
#include <time.h>
//#include "merge_sort.h"

using namespace std;
char c;

void basicMerge(int anArray[],int first, int mid, int last);
void RECURSIVEMERGESORT (int A[],int n);
void printArray(int[],int);

clock_t begin, stop;
long run_time;

int main() 
{
//main program

int maxSize = 20;
int theArray [maxSize];
//int B[maxSize];

cout<<"\nWelcome!!!";
cout<<"\nThis is a test of the mergesort procedure and their running times:";
   // <<endl<<"\nPlease enter the size of your array: ";
    
//cin>>maxSize;
//int first = 0;
//int mid = maxSize/2;
//int last = maxSize-1;

for (int i=0; i<maxSize; i++)
    theArray[i] =  rand()% 100+1;
	
printArray(theArray, maxSize);

begin = clock();
RECURSIVEMERGESORT(theArray, maxSize);
stop = clock();

cout<<endl<<"\nMergesort\n";

printArray(theArray, maxSize);

run_time = ((long) (stop - begin)) / CLOCKS_PER_SEC;

cout<<"\nRuntime of the procedure was "<<run_time<<endl;
cout<<"\nPlease press any key and enter to exit: ";
  
cin>>c;

return 0;
}

void printArray(int anArray[], int size) 
{
cout<<"\nThe array elements are now as follows: "<<endl;
for (int i = 0; i<size; i++)
	cout<<anArray[i]<<" ";
}
void basicMerge(int anArray[],int first, int mid, int last) {

     int f1 = first;
     int last1 = mid;
     int f2 = mid+1; 
     int last2 = last;
     
     int temp[last+1];
     
     int index = f1;
     for(; (f1<=last1)&&(f2<=last2); index++) {
           
            if(anArray[f2]<anArray[f1]) { 
               temp[index] = anArray[f2];
               f2++; }
            else {
                temp[index] = anArray[f1];
                f1++; }
            }// end for
                
    for(; (f1<=last1); f1++, index++)
          temp[index] = anArray[f1];

    for(; (f2<=last2); f2++, index++)
          temp[index] = anArray[f2];
          
    for(index = first; index<=last; index++)
              anArray[index] = temp[index];

}// end basicMerge

void RECURSIVEMERGESORT (int A[],int n) {
     int first=0;
     int last=n-1;
     //int temp[n];
     
	if (first<last) {
                  int mid =(first+last)/2;
                  int n2 = n-mid;
                  
                  RECURSIVEMERGESORT (A,mid);
                  RECURSIVEMERGESORT (A,n2);
                  
                  basicMerge(A, first, mid, last);
                  }
}// end RECURSIVEMERGESORT 


Hope that anyone can help me with this.

That's all thank you.
Topic archived. No new replies allowed.