实施特定的排序算法时,我遇到了一个问题,即使Google不想帮我...关于该算法的第一句话:
最大的一部分是将输入(数组)划分为三个部分:选择两个关键数字(红色和蓝色,断言红色
数组的排序应以递归方式进行:在对分区进行分区之前,先使用任意枢轴对输入进行分区,最后得到长度为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
更多:我想出了一个笨拙而笨拙的解决方案:
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);
}
}
最佳答案
每次调用分区方法时,都会发生一件简单的事情:
蓝色和红色索引处的项目正是它们所属的位置。
因此,当您调用代码的这一部分时:
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);
关于java - FlagSort-分而治之-小麻烦,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10672901/