Hi everybody,

I'm trying to figure out how much memory was used for merge sort algorithm. I believe it is just one or two expressions, but can't come up with them by my own.

   public void mergeSort (T[] data, int min, int max)
   {
      T[] temp;
      int index1, left, right;

      /** return on list of length one */
      if (min==max)
         return; 

      /** find the length and the midpoint of the list */
      int size = max - min + 1; // size of the array
      int pivot = (min + max) / 2; // middle point
      temp = (T[])(new Comparable[size]); 

      mergeSort(data, min, pivot); // sort left half of list
      mergeSort(data, pivot + 1, max); // sort right half of list

      /** copy sorted data into workspace */
      for (index1 = 0; index1 < size; index1++)
         temp[index1] = data[min + index1];

      /** merge the two sorted lists */
      left = 0;
      right = pivot - min + 1;
      for (index1 = 0; index1 < size; index1++)
      {
         if (right <= max - min)
            if (left <= pivot - min)
               if (temp[left].compareTo(temp[right]) > 0)
                  data[index1 + min] = temp[right++];

               else
                  data[index1 + min] = temp[left++];
            else
               data[index1 + min] = temp[right++];
         else
            data[index1 + min] = temp[left++];
      }
   }

Recommended Answers

All 4 Replies

Can you explain what you mean by

how much memory was used

What are you looking for?

Sorry, I had to be more specific.
Well I have a file that store some data. I use this method to sort the data. As far as I understand merge sort. It should use more memory, than the size of my original file. So i'm trying to figure out how much memory merge sort needs.

Oh this is great =) Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.