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

In summary: Your merge function is not working correctly because it is asking for too much memory. Try changing the size of the tempArray it is using.Thanks for the help.
  • #1
discoverer02
138
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

  • Array.txt
    1.4 KB · Views: 463
  • Arrayh.txt
    515 bytes · Views: 412
  • generic.txt
    5.1 KB · Views: 437
Last edited:
Physics news on Phys.org
  • #2
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?
 
  • #3
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"
 
  • #4
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 cout 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.
 
  • #5
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
 

What is a C++ array class?

A C++ array class is a user-defined data type that allows for the creation and manipulation of arrays in C++. It encapsulates the underlying array data structure and provides methods for accessing and modifying elements.

What are the advantages of using a C++ array class?

Using a C++ array class can help to simplify and streamline code, as it provides built-in methods for sorting, searching, and other common array operations. It also allows for better organization and encapsulation of data, making code more maintainable and reusable.

How do you declare and initialize a C++ array class?

To declare and initialize a C++ array class, you must first include the appropriate header file and then use the syntax "ArrayClass < data_type > array_name(size);" The size parameter indicates the number of elements in the array. You can then initialize the array by assigning values to each element using the array_name[index] notation.

What is sorting and why is it useful when working with arrays?

Sorting is the process of arranging elements in a specific order, such as ascending or descending. It is useful when working with arrays because it allows for easier searching and accessing of elements, as well as the ability to identify patterns and outliers in the data.

What are some commonly used sorting algorithms for C++ array classes?

Some commonly used sorting algorithms for C++ array classes include bubble sort, selection sort, insertion sort, quicksort, and mergesort. Each of these algorithms has its own advantages and disadvantages in terms of efficiency and implementation complexity, so the choice of which one to use will depend on the specific needs of the program.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
2
Views
377
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
Back
Top