0

这是一个使用归并排序的程序 - 从 1 到 1000 对 1000 个数字的列表进行排序。它显示原始列表,然后调用递归方法对其进行排序,然后显示它。

我在代码中不明白的是这两行:

MergeSort(数字,低,中);// 在方法中它会说“这里”

MergeSort(数字,中间+1,高);

我是java的初学者,这与我学到的一切背道而驰,因为我无法理解如何在方法中调用方法。除非它是我怀疑的对象。有人可以解释一下这两行代码的作用吗?

import java.io.*;
import java.util.*;
import java.text.*;

public class MergeSortExample
{
    static final int Max = 1000;

    static void MergeSort (int[] numbers, int lo, int n) // recursive method
    {

        int low = lo; // 0
        int high = n; // 999

        if (low >= high) // return case;
        {
            return;
        }

        int middle = (low + high) / 2;
        MergeSort (numbers, low, middle); // HERE
        MergeSort (numbers, middle + 1, high); // HERE

        int end_low = middle;
        int start_high = middle + 1;

        while ((lo <= end_low) && (start_high <= high))
        {
            if (numbers [low] < numbers [start_high])
            {
                low++;
            }
            else
            {
                int Temp = numbers [start_high];

                for (int k = start_high - 1 ; k >= low ; k--)
                {
                    numbers [k + 1] = numbers [k];
                }
                numbers [low] = Temp;
                low++;
                end_low++;
                start_high++;
            }
        }




    }


    public static void main (String str[]) throws IOException
    {
        BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
        DecimalFormat df = new DecimalFormat ("#");
        BufferedReader reader = new BufferedReader (new FileReader ("unsorted.txt"));
        //BufferedWriter writer = new BufferedWriter (new FileWriter ("test.txt", true)); // text to write


        int[] numbers = new int [Max];  // if its int the 0's in the beginiing would be cut off
        String line = null;
        int[] count = {0};

        int low = 0;
        int high = count [0] - 1;


        while ((line = reader.readLine ()) != null)
        {
            numbers [count [0]] = Integer.parseInt (line);
            System.out.println (numbers [count [0]]);

            count [0]++;
        }
        reader.close ();
        System.out.println ();
        System.out.println ("There are " + count [0] + " numbers.");
        System.out.println ();


        ///////////////////////////////////////////////////////////////////////////////


        MergeSort (numbers, 0, count [0] - 1);

        for (int i = 0 ; i < count [0] ; i++)
        {
            System.out.println (numbers [i]);
        }




    }
}
4

3 回答 3

1

It's basic recursion. A merge sort works by splitting the list into two parts, merge sorting each one, and then merging the two lists together. The part you are asking about is the part that merge sorts each of the two parts.

于 2013-01-08T02:33:46.657 回答
1

It is a recursive call of static method MergeSort(). Bad convention is used here. Names of methods should start with a lower case

于 2013-01-08T02:33:51.847 回答
0

Of course a method may invoke another method. The only confusing part here is that this method is static, which means it is invoked on a class instead of an object. Since it is called inside of the class it is defined in, you do not need to write MergeSortExample.MergeSort(). If you want to understand the differences between static and instance methods and fields, I suggest you look at the tutorials on the Java website.

于 2013-01-08T02:31:46.733 回答