【发布时间】:2019-01-29 05:39:49
【问题描述】:
我目前正在使用 Java 进行算法开发,最近确实陷入了一个特定问题。我一直面临开发两种不同算法的挑战。
我的任务是解决选择问题。选择问题确定一组 N 个数中第 k 个最大的数。
我已经成功实现了第一个算法。我将N个数字读入一个数组,通过一些简单的算法对数组进行降序排序,然后返回位置k的元素。
注意:k = N / 2
这是工作代码
public int selectionAlgorithmOne() {
int[] intArray = new int[]{1, 7, 9, 8, 2, 3, 5, 4, 6, 10};
//I sort the array of size N in decreasing order
bubbleSortDecreasingOrder(intArray);
//{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
//I obtain the value of k
int k = intArray.length / 2;
//I print the result
System.out.println(intArray[k]);
}
“5”中打印的值是正确的!然而,第二种算法有点棘手。
将前 k 个元素读入一个数组并按降序对它们进行排序。接下来,一个一个地读取每个剩余的元素。当一个新元素到达时,如果它小于数组中的第 k 个元素,则将其忽略。否则,它将被放置在数组中的正确位置,将一个元素从数组中剔除。当算法结束时,返回第 k 个位置的元素作为答案。
不幸的是,我的第二个算法不起作用。它返回错误的值“3”。它应该返回与第一个算法相同的“5”值,但效率更高。
我已经被困了几天,我真的很难找到解决方案。希望我已经为问题提供了足够的背景信息,如果我可以提供更多信息,请告诉我。提前致谢。
这是无效的代码
public int selectionAlgorithmTwo() {
int[] intArray = new int[]{1, 7, 9, 8, 2, 3, 5, 4, 6, 10};
int arrayLength = intArray.length;
int k = arrayLength / 2;
int[] firstHalf = new int[k];
//I read the first half of the elements into an array
for (int i = 0; i < k; i++) {
firstHalf[i] = intArray[i];
}
//I then sort the first half of the elements in decreasing order
bubbleSort(firstHalf);
for(int i = k; i < arrayLength; i++) {
int val = intArray[i];
//If the new element to insert is >= the kth largest
if (val > firstHalf[k - 1]) {
int pos = 0;
for(; pos < k; pos++) {
if(val > firstHalf[pos]) {
break; //I break once I have the correct position located
}
//I make the swap
for (int j = k - 1; j > pos; j--)
firstHalf[j] = firstHalf[j - 1];
firstHalf[pos] = val;
}
}
}
return firstHalf[k - 1];
}
【问题讨论】:
-
只是速写,我感觉这个
if(val > firstHalf[pos])应该是if(val < firstHalf[pos])。 -
谢谢你。不幸的是,该算法返回值 10 而不是预期的 5。
-
请在函数结束时以及前半部分刚刚被填充和排序时显示两个数组的内容。
-
请将其转为minimal reproducible example,并提供有关紧急情况下阵列状态的其他信息。
-
我认为第二个 for 循环不应该嵌套在第一个中。
标签: java arrays algorithm sorting