【问题标题】:MergeSort algorithm not sorting second halfMergeSort 算法不排序后半部分
【发布时间】:2013-11-13 16:48:04
【问题描述】:

数组的后半部分没有为我排序,我似乎不知道为什么。

这是我的代码

public static void sort(int[] a)
{
    if(a.length >= 2)
    {
        int halfLength = a.length / 2;
        int[] firstHalf = new int[halfLength];
        int[] lastHalf = new int[a.length - halfLength];

        divide(a, firstHalf, lastHalf);
        sort(firstHalf);
        sort(lastHalf);
        merge(a, firstHalf, lastHalf);
    }
}

private static void divide(int[] a, int[] firstHalf, int[] lastHalf)
{
    for(int i = 0; i < firstHalf.length; i++)
    {
        firstHalf[i] = a[i];
    }
    for(int i = 0; i < lastHalf.length; i++)
    {
        lastHalf[i] = a[firstHalf.length + i];
    }
}

private static void merge(int[] a, int[] firstHalf, int[] lastHalf)
{
    int firstHalfIndex = 0, lastHalfIndex = 0, aIndex = 0;
    while((firstHalfIndex <  firstHalf.length) && (lastHalfIndex < lastHalf.length))
    {
        if(firstHalf[firstHalfIndex] < lastHalf[lastHalfIndex])
        {
            a[aIndex] = firstHalf[firstHalfIndex];
            firstHalfIndex++;
        }
        else
        {
            a[aIndex] = lastHalf[firstHalfIndex];
            lastHalfIndex++;
        }
        aIndex++;

        while(firstHalfIndex < firstHalf.length)
        {
            a[aIndex] = firstHalf[firstHalfIndex];
            aIndex++;
            firstHalfIndex++;
        }
        while(lastHalfIndex < lastHalf.length)
        {
            a[aIndex] = lastHalf[lastHalfIndex];
            aIndex++;
            lastHalfIndex++;
        }
    }
}

我从教科书中得到这个,我知道我可以在网上找到更多的例子,但我希望我能先把它弄好。

输出:

Array values before sorting:
7 5 11 2 16 4 18 14 12 30  
Array values after sorting:
2 5 7 11 16 4 18 12 14 30

预期输出:

Array values before sorting:
7 5 11 2 16 4 18 14 12 30  
Array values after sorting:
2 4 5 7 11 12 14 16 18 30

感谢您的帮助!

【问题讨论】:

  • 调试器是你最好的朋友。

标签: java mergesort


【解决方案1】:

两个错误:

  • lastHalf[firstHalfIndex] 中的复制粘贴错误,应为 lastHalf[lastHalfIndex]
  • 第一个 while 循环不应跨越其他两个

更正:

private static void merge (int[] a, int[] firstHalf, int[] lastHalf) {

    int firstHalfIndex = 0, lastHalfIndex = 0, aIndex = 0;
    while ((firstHalfIndex < firstHalf.length) && (lastHalfIndex < lastHalf.length)) {
        if (firstHalf[firstHalfIndex] < lastHalf[lastHalfIndex]) {
            a[aIndex] = firstHalf[firstHalfIndex];
            firstHalfIndex++;
        } else {
            a[aIndex] = lastHalf[lastHalfIndex];
            lastHalfIndex++;
        }
        aIndex++;
    }
    while (firstHalfIndex < firstHalf.length) {
        a[aIndex] = firstHalf[firstHalfIndex];
        aIndex++;
        firstHalfIndex++;
    }
    while (lastHalfIndex < lastHalf.length) {
        a[aIndex] = lastHalf[lastHalfIndex];
        aIndex++;
        lastHalfIndex++;
    }
}

【讨论】:

    猜你喜欢
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 2011-07-04
    • 2017-05-11
    相关资源
    最近更新 更多