我的合并排序实现有什么问题?

问题描述

// merge operation for merge sort
private static void merge(int[] a,int left,int middle,int right) {
    int[] temp = new int[right - left + 1];        
    int leftCrawler = left,rightCrawler = middle;
    int currentIndex = 0;

    while (leftCrawler < middle && rightCrawler <= right) {
        if (a[leftCrawler] < a[rightCrawler])
            temp[currentIndex++] = a[leftCrawler++];
        else
            temp[currentIndex++] = a[rightCrawler++];
    }

    while (leftCrawler < middle)
        temp[currentIndex++] = a[leftCrawler++];

    while (rightCrawler <= right)
        temp[currentIndex++] = a[rightCrawler++];

    // copy temp into a
    for (int i = 0; i < temp.length; i++)
        a[i] = temp[i];
}

private static void mergeSort(int[] a,int right) {
    if (right > left) {
        int middle = left + (right - left) / 2;
        mergeSort(a,left,middle);
        mergeSort(a,middle + 1,right);
        merge(a,middle,right);
    }
}

public static void mergeSort(int[] a) {
    int left = 0,right = a.length - 1;
    mergeSort(a,right);
}

因此,我认为问题可能出在我的合并操作上,但是我在下面的数组int[] a = {2,5,7,15,8,9,10}中使用left = 0middle = 4right = a.length - 1以及合并操作成功完成了所需的工作。

我已经将我的mergeSort的实现与各种网站上的实现进行了比较,但我发现没有什么不同。我的mergeSort无法成功对数组进行排序。我在做什么错了?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)