Skip to main content
GameDev.net gamedev.net
🔒 Locked

[Java] Merge Sort

Started by alien3456 Oct 27, 2005 at 9:22 PM 3 replies 1k views
Original Post
alien3456
alien3456
I'm having trouble getting my merge sort to work. I get runtime errors. Not sure where I went wrong.

//    ******
        //    Merge
        //    ******  
         static Data[] Merge(Data array[])
        {
            int array_size = array.length;
            Data[] temp = new Data[array_size];
              doMergeSort(array, temp, 0, array_size - 1);
              return array;
        }
        static void doMergeSort(Data array[], Data temp[], int left, int right)
        {
              int mid;

              if (right > left)
              {
                mid = (right + left) / 2;
                doMergeSort(array, temp, left, mid);
                doMergeSort(array, temp, mid+1, right);

                array = merge(array, temp, left, mid+1, right);
              }
        }

        static Data[] merge(Data array[], Data temp[], int left, int mid, int right)
        {
              int i, left_end, num_elements, tmp_pos;

              left_end = mid - 1;
              tmp_pos = left;
              num_elements = right - left + 1;

              while ((left <= left_end) && (mid <= right))
              {
                if((array.name.compareTo(array[mid].name) < 0) || (array.name.compareTo(array[mid].name) == 0))
                {
                      temp[tmp_pos] = array;
                      tmp_pos = tmp_pos + 1;
                      left = left + 1;
                }
                else
                {
                      temp[tmp_pos] = array[mid];
                      tmp_pos = tmp_pos + 1;
                      mid = mid + 1;
                }
              }

              while (left <= left_end)
              {
                temp[tmp_pos] = array;
                left = left + 1;
                tmp_pos = tmp_pos + 1;
              }
              while (mid <= right)
              {
                temp[tmp_pos] = array[mid];
                   mid = mid + 1;
                tmp_pos = tmp_pos + 1;
              }

              for (i=0; i <= num_elements; i++)
              {
                    array = temp;
                right = right - 1;
              }
              return array;
        }   
I'm pretty sure it might have something to do with this little piece of code...

if((array.name.compareTo(array[mid].name) < 0) || (array.name.compareTo(array[mid].name) == 0))
But again, I'm not sure. If you want to run the whole program and see the runtime errors, <a href="http://paste.lisp.org/display/12966>the code is pasted here.
jfclavette
jfclavette
if((array.name.compareTo(array[mid].name) < 0) || (array.name.compareTo(array[mid].name) == 0))

is equivalent to:
if (array.name.compareTo(array[mid].name) <= 0)


Please close the string in your a tag. [smile]
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
alien3456
alien3456
Ah crap, I was really tired when I wrote the merge. I'll check tommorow to see if it works.
Zahlman
Zahlman
What sorts of runtime errors? Are you sure your initial Data[] is valid (in particular, that .name is non-null for each instance involved)?
MetalRob
MetalRob
This sounds a little too much like a homework question.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.