Thread: Trying to implement Merge Sort

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    1

    Trying to implement Merge Sort

    Hello Everyone,

    Im trying to implement merge sort. Currently Im trying to split the array[] in the prototype to induvidual arrays with one element. Im having a segmentation fault. In the console it keeps outputting the "I have been called statment" Which i put in to help me troubleshoot. Now I know for a fact that the array has 20 elements (comming from a text file) I have even printed all 20 elements and it stops after the last one. Can anyone please help me to find an error in my code?

    Go gently, im new to this. This is a school lab, but my prof is of no help with programming because he says its an algorithms course but we are also being asked to program. Recursion is also new to me.

    Thank you!

    Code:
    #include "mySort.h"
    #include <stdio.h>
    #include <stdlib.h>
    /**********************
    Algorithm to Implement
    MergeSort (Array(First..Last))
    Begin
     If Array contains only one element 
     Then Return Array
     Else     
      Middle= ((Last + First)/2) rounded down to the nearest integer 
       LeftHalfArray = MergeSort(Array(First..Middle))      
      RightHalfArray = MergeSort(Array(Middle+1..Last))      
      ResultArray = Merge(LeftHalfArray, RightHalfArray)      
      Return ResultArrayEndIfEnd MergeSort
    **********************/
    void mySort(int array[], unsigned int first, unsigned int last)
    { 
    
     /*Declare variables*/
     int middle;
     int size = last - first + 1;
     int arrayLeft[(size/2)];
     int arrayRight[(size/2)];
    
     /*Check if array contains one element*/
     if (first == last)
      return;
     /*If array contains more then 1 element, split them*/ 
     middle = (int) (size/2);
     printf ("I have been called, the middle is %d \n", middle) ; 
       
     mySort(arrayLeft, first, middle);
     mySort(arrayRight, (middle + 1), last); 
      
      
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    If you're going to use arrayLeft and arrayRight, you need to actually split the arrays, by copying the appropriate elements from array into arrayLeft and arrayRight. Also, look at the comment block above your function, middle should not be size/2.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    So if size is 11, consider how big are you really going to need to make arrayLeft or arrayRight?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Merge sort fails to sort
    By Cresh07 in forum C++ Programming
    Replies: 3
    Last Post: 09-23-2009, 11:17 AM
  2. Quick Sort or Merge Sort???
    By swanley007 in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2005, 06:48 PM
  3. Quick sort VS Merge Sort
    By sachitha in forum Tech Board
    Replies: 7
    Last Post: 09-03-2004, 11:57 PM
  4. merge sort and selection sort and time spent on both
    By misswaleleia in forum C Programming
    Replies: 3
    Last Post: 06-04-2003, 02:24 PM
  5. help with merge sort
    By maloy in forum C Programming
    Replies: 4
    Last Post: 03-04-2002, 06:22 PM