FlagSort - 分而治之 - 小麻烦

时间:2012-05-20 11:09:44

标签: java sorting recursion stack-overflow divide-and-conquer

实施特定的排序算法,我遇到了一个问题,即使谷歌不想帮我...首先关于算法的一些话

狮子的分享是将输入(数组)划分为三个部分:挑选两个关键数字(红色和蓝色,断言红色< =蓝色),分区的元素将(1)小于两者,( 2)在两者之间的某处和(3)比两个枢轴都大。这是工作正常的部分!

应该递归地对数组进行排序:在对分区进行分区之前使用任意枢轴对输入进行分区,从而最终得到长度为1或2的粒子,这些粒子按照def进行排序。或者更确切地说,可以很容易地分类。

问题是现在长度为> = 3的分区由一个键值组成:如果再次分区,无论选择哪个枢轴,所有元素都会在之后放入同一个分区,最终运行进入堆栈溢出。 这就是为什么我想到你能够帮助我,,因为我确信有一个解决方案。

强制性JavaCode代码段:分区 - 对于德语调试感到遗憾,也懒得翻译它。 IntTuple只能容纳两个整数;没什么太荒谬的。

public static IntTuple partition(int[] E, int left, int right, int red, int blue){
    if (red > blue) {
        int v = red;
        red = blue;
        blue = v;
    }
    System.out.println("Partition Eingabe: " + Arrays.toString(E) + " Links=" + left + " Rechts=" + right + " Rot=" + red + " Blau=" + blue);
    /*
     * Es gilt r <= b, also gilt für beliebige x...
     * ... x < r => x < b
     * ... x > b => x > r.
     *
     * Das Gerüst für diesen Algorithmus wurde kopiert von Folie 7-13
     */
    IntTuple result = new IntTuple (left, right); // rote und blaue Regionen sind leer
    int u = left; // weiße Region ist leer, die unbekannte == E[left...right]
    while (u <= result.v2) {
        System.out.print("E[" + u + "]: ");
        if (E[u] < red) {
            System.out.print("rot  ");
            swap(E, result.v1, u);
            result.v1++; // vergrößere die rote Region
            u++; // verkleinere die unbekannte Region
        } else if ((E[u] >= red) && (E[u] <= blue)) {
            System.out.print("weiß ");
            u++; // verkleinere die unbekannte Region
        } else if (E[u] > blue) {
            System.out.print("blau ");
            swap(E, result.v2, u);
            result.v2--; // vergrößere die blaue Region
        }
        System.out.print("Partition Schritt: [");
        for(int i = left; i < right; i++)
            System.out.print("" + E[i] + " ");
        System.out.println("" + E[right] + "]");
    }
    System.out.print("Partition Ausgabe: [");
    for(int i = left; i < right; i++)
        System.out.print("" + E[i] + " ");
    System.out.println("" + E[right] + "]" + " RotG=" + result.v1 + " BlauG=" + result.v2);
    return result;
}

强制性JavaCode代码段:排序

private static void flagSort(int[] E, int left, int right){
    System.out.println("Sortiere: " + left + " bis " + right);
    if(left < right - 1) {
        Random rnd = new Random();
        IntTuple v = partition(E, left, right, rnd.nextInt(50), rnd.nextInt(50));
        //IntTuple v = partition(E, left, right, E[left], E[left + 1]);
        flagSort(E, left, v.v1 - 1);
        flagSort(E, v.v1, v.v2);
        flagSort(E, v.v2 + 1, right);
    } else if((left == right - 1) && (E[left] > E[right])) {
        swap(E, left, right);
    }
}

非常感谢任何想法!

问候,LDer

更多:我想出了这个hacky and awkward解决方案:

private static void flagSort(int[] E, int left, int right, boolean dual){
    if(left < right) { // Singleton or empty segments are already sorted!
        IntTuple v;
        if(dual) // The last step has produced only a single white partition.
            // Treat this partition with double pivot
            v = partition(E, left, right, E[left], E[left]);
        else    // The last step has produced more than one partition, go on normally.
            v = partition(E, left, right, E[left], E[left + 1]);
        // Analyze partitions
        if((left != v.v1) || (right != v.v2)) {
            // 2 or 3 partitions available. Descend further.
            flagSort(E, left, v.v1 - 1, false);
            flagSort(E, v.v1, v.v2, false);
            flagSort(E, v.v2 + 1, right, false);
        } else if(!dual) {
            // Only the white partition is not empty, partition it with double pivot
            flagSort(E, v.v1, v.v2, true);
        } // Last case: The only not-empty partition is white after partitioning with double pivot.
          // Description of the algorithm immediately implies that this consists of only one key value, thus is sorted.
    }
}

任何人都可以帮助创建更易读的版本吗?

更多:这个看起来更好:

private static void flagSort(int[] E, int left, int right, int offset){
    if(left < right) { // Singleton or empty segments are already sorted!
        IntTuple v = partition(E, left, right, E[left], E[left + offset]);
        // Analyze partitions
        if ((left != v.v1) || (right != v.v2)) {
            // 2 or 3 partitions available. Descend further.
            flagSort(E, left, v.v1 - 1, 1);
            flagSort(E, v.v1, v.v2, 1);
            flagSort(E, v.v2 + 1, right, 1);
        } else if (offset > 0)
            // Only the white partition is not empty, partition it with double pivot
            flagSort2(E, v.v1, v.v2, 0);
        // Last case: The only not-empty partition is white after partitioning with double pivot.
        // Description of the algorithm immediately implies that this consists of only one key value, thus is sorted.
    }
}

特别感谢toto2,即使我没有明确地传递红色/蓝色!

更多:更多随机性,因为toto2再次完全有一点:

private static void flagSort(int[] E, int left, int right, int offset){
    if(left < right) {
        IntTuple v = partition(E, left, right, E[left + (offset % (right - left))], 
            E[left + ((2 * offset) % (right - left))]);
        if ((left != v.v1) || (right != v.v2)) {
            int random = rnd.nextInt(right - left);
            flagSort(E, left, v.v1 - 1, random);
            flagSort(E, v.v1, v.v2, random);
            flagSort(E, v.v2 + 1, right, random);
        } else if (offset > 0)
            flagSort(E, v.v1, v.v2, 0);
    }
}

2 个答案:

答案 0 :(得分:0)

每次调用分区方法时,都会发生一件简单的事情: 蓝色和红色索引的项目正是它们所属的位置。 所以,当你调用这部分代码时:

    flagSort(E, left, v.v1 - 1);
    flagSort(E, v.v1, v.v2);
    flagSort(E, v.v2 + 1, right);

这些来电不得包含蓝色和红色, 换句话说:

 flagSort(E, left, v.v1 - 1);
 flagSort(E, v.v1 +1 , v.v2 -1);
 flagSort(E, v.v2 + 1, right);

答案 1 :(得分:0)

IntTuple v = partition(E, left, right, rnd.nextInt(50), rnd.nextInt(50));

50?您知道当前分区的最小 - 最大(红 - 蓝)值,这是一个比0-49更窄的范围。您应该修改flagSort,这样您不仅可以传递左右值,还可以传递红蓝色值。

如果使用red == blue调用flagSort,则可以返回,因为子数组具有相同的值并且已经排序。