C++: Problem w/Array class passed to quicksort and mergesort

Discussion in 'Software' started by discoverer02, Jun 5, 2004.

  1. discoverer02

    discoverer02 Private E-2

    I'm having a devil of a time 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. The program was created in MS Visual Studio.NET 2003 with Windows 2000 OS.

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

    Thanks for the help

    discoverer02
     

    Attached Files:

  2. Wyatt_Earp

    Wyatt_Earp MajorGeek

    You got the code for the mergesort straight out of the textbook, right? Also, what does the mergesort do? Infinite Loop? Incorrect sorting?
     
  3. discoverer02

    discoverer02 Private E-2

    Yes, I pulled the mergeSort and merge functions straight out of the text book. 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 occuring in the merge function.

    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. discoverer02

    discoverer02 Private E-2

    Everything seems to work just fine for my Array objects with a length of 3 or less.
     
  5. Wyatt_Earp

    Wyatt_Earp MajorGeek

    Can you run the debugger and find out exactly where the Merge function is crashing?
     
  6. discoverer02

    discoverer02 Private E-2

    Thank for your help. I figured out what was wrong. The textbook code for MERGE() isn't very efficient. The original code was setting the length of the tempArray to MAXITEMS, whatever that is. I was setting it to lastindex - first index, but the code also set the initial index for the tempArray to first index. The way the books code works, I have to create a much larger array then needed to keep everything in sink during the sorting, so I changed the size of the tempArray to last index + 1.


    It's working fine now.
     
  7. Wyatt_Earp

    Wyatt_Earp MajorGeek

    Cool, glad you got it fixed. :)
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds