Why is my mergesort function only working for arrays up to three items?

Click For Summary
SUMMARY

The discussion centers on a mergesort implementation that fails for arrays larger than three items. The user implemented an array class in MS Visual Studio.NET 2003 and encountered a crash during the merge function when processing arrays of four or more elements. The issue was traced to an inefficiently sized temporary array in the textbook's Merge() function, which was corrected by adjusting the size to match the maximum index passed to the function. This adjustment resolved the crashing issue, allowing the mergesort to function correctly for larger arrays.

PREREQUISITES
  • Understanding of mergesort algorithm and its recursive nature
  • Familiarity with array data structures in programming
  • Knowledge of memory management and potential out-of-bounds errors
  • Experience with MS Visual Studio.NET 2003 environment
NEXT STEPS
  • Review the implementation of the mergesort algorithm in C++
  • Learn about memory management techniques in C++ to avoid runtime errors
  • Explore efficient array handling and resizing strategies
  • Investigate debugging techniques for recursive functions in programming
USEFUL FOR

Programmers, software developers, and computer science students looking to understand mergesort implementation, memory management, and debugging techniques in C++.

discoverer02
Messages
138
Reaction score
1
I'm not sure that this post belongs here, but I'll give it a try. I'm having a devil of a time :devil: trying to figure out why my program won't work.

The assignment was to create and array class consisting of:
A constructor that takes and integer for the size of the array
A constructor that takes 2 integers, one for the first index and one for the last index
A copy constructor
Overload []

We were then supposed to pass array objects to a quicksort and mergesort function that came right out of our textbook.

The quicksort function is working just fine with my array class, but the mergesort only works for arrays up to three items. If I try to pass an array object of length 4 or greater the merge function fails.

I've spent hours trying to debug to no avail. :cry: The program was created in MS Visual Studio.NET 2003

I'd really appreciate it if a programmer could take a look at it and fill me in on where I went wrong.

Thanks for the help

discoverer02
 

Attachments

Last edited:
Physics news on Phys.org
Can you tell us exactly what the problem is? Is your mergesort just not sorting properly, or do you get a runtime error and the program just stops, or do you get strange output?
 
Sorry about that. I should have been much more specific.

I pulled the mergeSort and merge functions straight out of the textbook. The only difference is one parameter which is my Array class and the tempArray created in Merge() which is also my Array class.

The mergeSort function is supposed to recursively break the array into smaller and smaller halfs and then the merge function is supposed to put the array back together sorted from lowest to highest. The error is apparently occurring in the merge function.

Everything seems to work just fine for arrays of lengths 0 to 3.

For an array of 4 strings, it completes the merge function once and then apparently crashes there on the second pass.

For the array of 4 strings, the recursive calls go:
MergeSort
MergeSort
Merge
MergeSort
and then it seems to crash in Merge

This is the message that Windows 2000 gives me:

The instruction at "0x0043bf84" referenced memory at "0x000d008b". The memory could not be "written"
 
First of all, did you say you tested your sort for arrays of size 0?

It seems that somewhere your merge sort is trying to access memory outside your array. Look at the places where the function could be trying to write to memory, and see if maybe it's trying to write to the fifth index or something.

Mergesort (0,3)
--> Mergesort (0,1)
-----> Mergesort (0,0) * does not say "entered mergesort" *
-----> Mergesort (1,1) * does not say "entered mergesort" *
-----> Merge (0,0,1,1)
--> Mergesort (2,3)
-----> Mergesort (2,2) * does not say "entered mergesort" *
-----> Mergesort (3,3) * does not say "entered mergesort" *
-----> Merge (2,2,3,3)
--> Merge (0,1,2,3)

I would just have a bunch of count statements checking what indexes are being worked with, in each while/if block of code to pinpoint where the problem is. I can't see why it would successfully perform the first merge and not the second. Well, the second merge does deal with the end of the array, so perhaps you're going out of bounds there.

I looked at the code, it looks like it should work.
 
Thanks AKG.

I figured it out. The book's Merge() function isn't written very efficiently. It requires a tempArray much larger than necessary in order to keep the items in the different arrays in sync during the sorting process. I changed the size of the array from the actual number of items being sorted to the maximum index number being passed to the function.

It works fine now.

Thanks again for your help. It's much appreciated.

discoverer02
 

Similar threads

  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
5K
Replies
2
Views
2K
  • · Replies 89 ·
3
Replies
89
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K