首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >合并排序Java算法

合并排序Java算法
EN

Stack Overflow用户
提问于 2015-04-03 14:45:34
回答 2查看 278关注 0票数 0

嘿,我的合并类型一直有问题,我知道网上有很多信息,这是多次出现的,但我不知道发生了什么,无论我做什么,这都不会起作用,一些帮助将不胜感激。

我的主要方法如下:

代码语言:javascript
复制
for (int i = 0; i < trials; i++) {
        data = generateRandomArray(arraySize);

        // You can use following line to print out data for debugging
        System.out.println("The array is: " + getString(data));

        // ADD YOUR CODE HERE TO SORT DATA AND CALCULATE EXECUTION TIME

        // System.out.println("first index:" + data[0]);
        // System.out.println("first index:" + data[arraySize-1]);

        //System.out.println("hello  " + SortArray.basicPartition(data,0,data.length-1));
        SortArray.mergeSort(data, 0, data.length-1);

        if (isSorted(data))
            System.out.println("   passes -  array is sorted");
        else
            System.out.println("   failed -  array is not sorted");
代码语言:javascript
复制
public static <T extends Comparable<? super T>>
void mergeSort(T[] a, T[] tempArray, int first, int last){
    if(first < last)        // We have some work to do
    {
        int mid = first+(last-first)/2;
        mergeSort(a, tempArray, first, mid);
        mergeSort(a, tempArray, mid+1, last);
        merge(a, tempArray, first, mid, last);
    }
} // end mergeSort

/** Merge the entries in two contiguous sorted sublists 
 * @param a An array of Comparable objects.
 * @param tempArray A temporary array used in the merge.
 * @param first An integer >= 0 and < mid.
 * @param mid An integer  <= last.
 * @param last An integer  < a.length.
 */
public static <T extends Comparable<? super T>>
void merge(T[] a, T[] tempArray, int first, int mid, int last){

    int firstIndex = first;
    int FirstHalfEnd = mid -1;


    while ((first <= FirstHalfEnd) && (mid <= last)) {

        if (a[first].compareTo(a[mid]) <= 0) {

            tempArray[firstIndex] = a[first]; // last to first
            firstIndex++;
            first++;
        } 
        else {
            tempArray[firstIndex] = a[mid];
            FirstHalfEnd++;
            mid++;
            //System.out.println("out of bounds");
        }
    }

    while (first <= FirstHalfEnd) {
        tempArray[firstIndex] = a[first];
        firstIndex++;
        first++;

    }
    while(mid <= last){
        tempArray[firstIndex] = a[mid];
        firstIndex++;
        mid++;
    }
    for(int i=0;i<(last-first+1);i++){ 
        a[last] = tempArray[last];
        last--;
        //System.out.println(a[i]);
    }

} // end merge

输出

代码语言:javascript
复制
The array is: [ 1 5 3 5 1 6 9 7 1 4 ]
   failed -  array is not sorted
The array is: [ 1 8 3 4 3 1 6 8 0 9 ]
   failed -  array is not sorted
The array is: [ 0 1 5 5 5 0 0 3 0 4 ]
   failed -  array is not sorted
The array is: [ 0 0 6 2 7 4 6 2 2 2 ]
   failed -  array is not sorted
The array is: [ 4 9 2 3 3 4 4 0 3 5 ]
   failed -  array is not sorted
EN

回答 2

Stack Overflow用户

发布于 2015-04-03 15:22:55

我还没有运行您的代码--有遗漏的部分-,但是我在merge()函数的第一个while循环中发现了两个问题--请参阅添加的注释:

代码语言:javascript
复制
while ((first <= FirstHalfEnd) && (mid <= last)) {

    // compareTo return a negative value if (a[first] < a[mid])
    // Then I think your comment is wrong: the values are put in the 
    // temporary array in increasing order. It means you have to review
    // the for loop that copies the values 
    // at the end.
    if (a[first].compareTo(a[mid]) <= 0) {

        tempArray[firstIndex] = a[first]; // last to first (No!)
        firstIndex++;
        first++;
    } 
    else {
        tempArray[firstIndex] = a[mid];
        FirstHalfEnd++; // <= this a bug, should be firstIndex++
        mid++;
        //System.out.println("out of bounds");
    }
}

编辑

由于tempArray中的值是递增的,所以复制for循环应该是这样的:

代码语言:javascript
复制
for(int i = first; i <= last; ++){ 
    a[i] = tempArray[i];
}

哪一个可以简化?或通过

代码语言:javascript
复制
System.arraycopy(tempArray, first, a, first, (last-first+1));
票数 1
EN

Stack Overflow用户

发布于 2015-10-02 20:01:53

这里有一个替代实现。它按降序排列:

代码语言:javascript
复制
public class MergeSort {
public static void merge(int[]a,int[] aux, int f, int m, int l) {

    for (int k = f; k <= l; k++) {
        aux[k] = a[k];
    }

    int i = f, j = m+1;
    for (int k = f; k <= l; k++) {
        if(i>m) a[k]=aux[j++];
        else if (j>l) a[k]=aux[i++];
        else if(aux[j] > aux[i]) a[k]=aux[j++];
        else a[k]=aux[i++];
    }       
}
public static void sort(int[]a,int[] aux, int f, int l) {
    if (l<=f) return;
    int m = f + (l-f)/2;
    sort(a, aux, f, m);
    sort(a, aux, m+1, l);
    merge(a, aux, f, m, l);
}
public static int[] sort(int[]a) {
    int[] aux = new int[a.length];
    sort(a, aux, 0, a.length-1);
    return a;
}

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29434372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档