为什么合并排序不稳定? [英] why is in place merge sort not stable?

查看:178
本文介绍了为什么合并排序不稳定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的实现是稳定的,因为它在标记为XXX的行使用< = 而不是< 。这也使它更有效率。有没有理由在这一行使用< 而不是< =

The implementation below is stable as it used <= instead of < at line marked XXX. This also makes it more efficient. Is there any reason to use < and not <= at this line?

/**
class for In place MergeSort
**/
class MergeSortAlgorithm extends SortAlgorithm {
    void sort(int a[], int lo0, int hi0) throws Exception {
    int lo = lo0;
    int hi = hi0;
    pause(lo, hi);
    if (lo >= hi) {
        return;
    }
    int mid = (lo + hi) / 2;

        /*
         *  Partition the list into two lists and sort them recursively
         */
        sort(a, lo, mid);
        sort(a, mid + 1, hi);

        /*
         *  Merge the two sorted lists
         */
    int end_lo = mid;
        int start_hi = mid + 1;
    while ((lo <= end_lo) && (start_hi <= hi)) {
            pause(lo);
        if (stopRequested) {
                return;
            }
            if (a[lo] <= a[start_hi]) {                   // LINE XXX
                lo++;
            } else {
                /*  
                 *  a[lo] >= a[start_hi]
                 *  The next element comes from the second list, 
                 *  move the a[start_hi] element into the next 
                 *  position and shuffle all the other elements up.
                 */
        int T = a[start_hi];
                for (int k = start_hi - 1; k >= lo; k--) {
                    a[k+1] = a[k];
                    pause(lo);
                }
                a[lo] = T;
                lo++;
                end_lo++;
                start_hi++;
            }
        }
    }

    void sort(int a[])  throws Exception {
    sort(a, 0, a.length-1);
    }
}


推荐答案

因为代码中的< = 确保不会交换相同值的元素(在排序数组的左半部分和右半部分)。
而且,它避免了无用的交换。

Because the <= in your code assures that same-valued elements (in left- and right-half of sorting array) won't be exchanged. And also, it avoids useless exchanges.

if (a[lo] <= a[start_hi]) {
 /* The left value is smaller than or equal to the right one, leave them as is. */
 /* Especially, if the values are same, they won't be exchanged. */
 lo++;
} else {
 /*
  * If the value in right-half is greater than that in left-half,
  * insert the right one into just before the left one, i.e., they're exchanged.
  */
 ...
}

假设相同两半中的值元素(例如,'5')和上面的运算符是<
如上面的评论所示,右边的'5'将插入左'5'之前,换句话说,将交换相同值的元素。
这意味着排序不稳定。
此外,交换相同价值元素的效率也很低。

Assume that same-valued element (e.g., ‘5’) in both-halves and the operator above is <. As comments above shows, the right ‘5’ will be inserted before the left ‘5’, in other words, same-valued elements will be exchanged. This means the sort is not stable. And also, it's inefficient to exchange same-valued elements.

我认为效率低下的原因来自于算法本身。
您的合并阶段是使用插入排序实现的(如您所知,它是O(n ^ 2))。

I guess the cause of inefficiency comes from the algorithm itself. Your merging stage is implemented using insertion sort (as you know, it's O(n^2)).

您可能需要重新实施排序巨大的阵列。

You may have to re-implement when you sort huge arrays.

这篇关于为什么合并排序不稳定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆