Java合并排序错误?继续获取输入值?

时间:2015-11-07 05:37:50

标签: java sorting merge

我是Java排序算法的新手,我在合并排序算法中找不到错误。结果不断给出输入数组,而不是排序结果。任何帮助或建议将不胜感激。

public class Mergesort {

    static int [] finalarray = new int[12];

    public static void main(String [] args)
    {
        int [] numbers = {6,10,15,4,30,60,24,90,76,100,120,140};

        int [] tem = new int[12];

        mergesort(numbers, tem, 0, (numbers.length - 1));

        for (int i = 0; i < 12; i++)
        {
            System.out.println("" +finalarray[i]);
        }
    }

    public static void mergesort(int [] num, int [] temp, int first,int last)
    {
        if (first < last)
        {
            int mid = (first + last) / 2;
            mergesort(num, temp, first,mid);
            mergesort(num, temp, mid + 1, last);
            merge(num,temp,first,mid,last);
        }
    }

    public static void merge(int [] num, int [] temp, int firstpos, int midpos, int lastpos)
    {
        int half1begin = firstpos;
        int half1end = midpos;
        int half2begin = midpos + 1;
        int half2end = lastpos;
        int numbersIndex = 0;


        while(half1begin <= half1end && half2begin <= half2end)
        {
            if (num[half1begin] <= num[half2begin])
            {
                temp[numbersIndex] = num[half1begin];
                ++half1begin;
            }
            else 
            {
                temp[numbersIndex] = num[half2begin];
                ++half2begin;
            }

            numbersIndex++;
        }

        while(half1begin <= half1end )
        {
            temp[numbersIndex++] = num[half1begin++];
        }

        while(half2begin <= half2end )
        {
            temp[numbersIndex++] = num[half2begin++];
        }

        for (int i = 0; i < 12; i++)
        {
            finalarray[i] = temp[i];
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我将合并方法更改为以下内容:

public static void merge(int [] num, int [] temp, int firstpos, int midpos, int lastpos)
{
    int half1begin = firstpos;
    int half2begin = midpos + 1;
    int numbersIndex = firstpos;

    //Copies the original array into the temp array
    for (int i = 0; i < 12; i++) {
      temp[i] = num[i];
    }

    //Copies the smallest value from either side to the original array
    while((half1begin <= midpos) && (half2begin <= lastpos))
    {
      if (temp[half1begin] <= temp[half2begin])
        {
            num[numbersIndex] = temp[half1begin];
            ++half1begin;
        }
        else 
        {
            num[numbersIndex] = temp[half2begin];
            ++half2begin;
        }

        numbersIndex++;
    }

    //Copies the rest of the left side of the array into original
    //No need to copy the right side
    while(half1begin <= midpos )
    {
      num[numbersIndex++] = temp[half1begin++];
    }
}

主要的变化是每一轮将临时数组初始化为num数组,并在while循环中交换num和temp的顺序。

您使用的原始方法不起作用,因为用于在while循环中进行比较的数组在每次运行时都是完全相同的,因此它没有做任何事情。

此外,您不需要创建新的finalarray来存储值;在main方法中从数字数组中打印它就足够了。如果您愿意,可以考虑使用arraycopy复制到最终数组。

...如果我有任何这些错误,请道歉,因为我也不是那么精通。

Source