是否每次在此mergesort实现中调用merge

时间:2013-02-26 21:49:30

标签: java mergesort

在下面的代码中,每次在mergesort方法的条件if (low < high) {内调用合并?我认为它每次都被称为递归mergesort方法的一部分?

package sorting;

public class MyMergeSort extends Print{

      private static int[] numbers;
      private static int[] helper;

      private static int number;

    public static void main(String args[]){

        int[] array = {1 , 3 , 5 , 7 , 8};

        sort(array);
    }

      public static void sort(int[] values) {
            numbers = values;
            number = values.length;
            helper = new int[number];
            mergesort(0, number - 1);
          }

      private static void mergesort(int low, int high) {
            // Check if low is smaller then high, if not then the array is sorted
            if (low < high) {
              // Get the index of the element which is in the middle

              int middle = low + (high - low) / 2;
              println("low is "+low); println("high is "+high); println("middle is "+middle);
              // Sort the left side of the array
              mergesort(low, middle);
              // Sort the right side of the array
              mergesort(middle + 1, high);

              // Combine them both
              merge(low, middle, high);
            }
      }

      private static void merge(int low, int middle, int high) {

            // Copy both parts into the helper array
            for (int i = low; i <= high; i++) {
              helper[i] = numbers[i];
            }

            int i = low;
            int j = middle + 1;
            int k = low;
            // Copy the smallest values from either the left or the right side back
            // to the original array
            while (i <= middle && j <= high) {
              if (helper[i] <= helper[j]) {
                numbers[k] = helper[i];
                i++;
              } else {
                numbers[k] = helper[j];
                j++;
              }
              k++;
            }
            // Copy the rest of the left side of the array into the target array
            while (i <= middle) {
              numbers[k] = helper[i];
              k++;
              i++;
            }

          }
}

2 个答案:

答案 0 :(得分:1)

                           mergesort(1,3) (1)
                           /             \
                          / (2)           \ (5)
                         v                 v
              mergesort(1,2)              mergesort(2,3)
             /        \                   /            \
    (3)     /          \   (4)       (6) /              \  (7)
  mergesort(1,1)     mergesort(2,2)    mergesort(2,2)   mergesort(3,3)
            \        /                           \      /
             \      /                             \    /
               merge (8)                           merge (9)
                    \_________>  merge  <__________/
                                 (10)

您可以看到,只要low < high呼叫merge,我就会对示例进行编号,以便您可以更好地了解何时进行呼叫。

答案 1 :(得分:0)

条件

if (low < high)

终止条件。当达到该条件时(例如,当high = low时),该方法将简单地返回,而不是继续进行任何递归调用。

除了递归终止之外,是的,每次调用mergesort方法时都会调用一次合并。