Reputation: 51
I am relatively new to programming in C/C++ and I was practicing by developing code for merge-sort.
But when I call the mergeArrays() function in my code, program execution stops. I am using Eclipse IDE.
Situation 1 - Now, from sort(), if I call mergeArrays() program execution stops. I can reach only Debug Point1, eclipse hangs before it reaches Debug point 2 or 3.
Situation 2 - However, when do not call mergeArrays() from sort(), by commenting out the the call to mergeArrays. Program executes to completion (obviously, I do get the desired result.) In situation 2, I can reach all 3 Debug Points.
I am not worried about correctness of code, but more interested in trying to find out why is call mergeArrays() from sort() affecting call to sort() itself in situation 1.
Here is code
int main(){
int Arr = {3,2,4,1,5,6} ;
int ArraySize = 6
CMergeSort mergesort;
cout << "Debug Point 1" << endl;
mergesort.sort(Arr,ArraySize);
cout << "Debug Point 3 " << end;
// Print the Sorted List
for(int k=0; k < ArraySize ;k++)
{
cout << Arr[k] << endl ;
}
return 0;
}
class CMergeSort{
public:
int* sort(int *mArray,int mArraySize);
int* mergeArrays(int* leftArray,int leftArraySize, int* rightArray,int rightArraySize, int* mArr);
};
int* CMergeSort::sort(int* mArray, int mArraySize)
{
if(mArraySize < 2)
{
return mArray ;
}
cout << "Debug Point 2 "<< endl ;
int mid = mArraySize/2 ;
int left_array[mid] = {} ;
int right_array[mArraySize - mid] = {};
for(int i = 0 ; i< mid;i++) // left array size = mArraySize - mid
{
left_array[i] = mArray[i] ;
}
for(int j = 0 ;j< mArraySize - mid;j++) // right arr size= mArraySize - mid
{
right_array[j] = mArray[mid+j] ;
}
sort(left_array,mid);
sort(right_array, mArraySize - mid);
mergeArrays(left_array, mid, right_array, mArraySize - mid, mArray);
return mArray;
}
int* CMergeSort::mergeArrays(int* leftArray,int leftArraySize, int* rightArray,int rightArraySize, int* mArr)
{
int i = 0 ; // left array tracker
int j = 0 ; // right array tracker
int k = 0 ; // main array tracker
for(int s =0 ; s < leftArraySize + rightArraySize; i++)
{
if(leftArray[i] < rightArray[j] || j == leftArraySize + rightArraySize)
{
mArr[i+j] = leftArray[i] ;
i++ ;
k++ ;
}
if (leftArray[i] > rightArray[j] || i == leftArraySize + rightArraySize )
{
mArr[i+j] = rightArray[j];
j++;
k++;
}
}
return mArr ;
}
Upvotes: 0
Views: 70
Reputation: 51
Thanks for your replies everyone,
I made two mistakes. First,
for(int s =0 ; s < leftArraySize + rightArraySize; i++)
i++ should be replaced by s++
Secondly,
if(leftArray[i] < rightArray[j] || j == rightArraySize + leftArraySize) should be replaced by if(leftArray[i] < rightArray[j] || j == rightArraySize)
As pointed out earlier, i++ is causing indefinite loop that crashes the program.
But I am still curious, that why can't reach DEBUG POINT 2 ?
DEBUG POINT 2 in Sort(), comes before we enter indefinite loop in MergeSort(), Shouldn't the program crash after at-least printing DEBUG POINT 2 in output.
Can anyone shed light on that ?
Upvotes: 0
Reputation: 66230
Look at your for
cycle in mergeArrays()
for(int s =0 ; s < leftArraySize + rightArraySize; i++)
You define a cycle variable, s
; you test the loop condition against s
but you increment another variable, i
.
Look at the full cycle and you can see that you never modify s
.
So the cycle should continue forever.
But I'm surprised that the program doesn't crash, because it could write in mArr[i+j]
, with i
very high.
p.s.: sorry for my bad English
Upvotes: 1