【发布时间】:2018-03-13 02:06:30
【问题描述】:
我已经获得了我的分区算法和 ksmall 递归方法来(几乎)完全计算...我只是遇到了一个情况,我相信我的分区算法没有处理随机给定的重复数字大批。任何其他数组和我的输出都可以完美运行。
递归和分区检查将所有小于枢轴的数字放在左侧,将所有大于枢轴的数字放在右侧。
如何处理随机数组中的重复数字?
这是我的代码:
private static int partition(int[] theArray, int first, int last) {
// Returns the index of the pivot element after partitioning
// theArray[first..last]
int p = theArray[first]; // use the first item of the array as the pivot (p)
int lastS1 = first; // set S1 and S2 to empty
int lastS2 = theArray.length-1;
while (lastS1 < lastS2) {
for (; theArray[lastS1] < p; lastS1++) ;
for (; theArray[lastS2] > p && lastS2 > 0; lastS2--) ;
if (lastS1 < lastS2)
swap(theArray, lastS1, lastS2);
}
swap(theArray, lastS1, lastS2);
return lastS2;
}
public static int kSmall(int k, int[] anArray, int first, int last) {
int pivotIndex = partition(anArray, first, last);
int p = anArray[pivotIndex]; // p is the pivot
if (pivotIndex == k - 1)
return anArray[pivotIndex];
if (p > anArray[k - 1])
return kSmall(k, anArray, first, pivotIndex - 1);
return kSmall(k, anArray, pivotIndex + 1, last);
在一些示例中进行更深入的解释:
以下是成功运行的一些示例:
array = [13 | 29 | 53 | 49 | 68 | 12 | 72 | 47 | 80 | 89]
--------------------------------------------------------------------
Please enter an integer k, 1<=k<=10, or 'R' to refill the array:
1
Kth smallest is: 12
With K as: 1
还有……
array = [60 | 45 | 27 | 20 | 4 | 80 | 75 | 59 | 78 | 41]
--------------------------------------------------------------------
Please enter an integer k, 1<=k<=10, or 'R' to refill the array:
1
Kth smallest is: 4
With K as: 1
不成功运行:
array = [68 | 77 | 32 | 54 | 30 | 83 | 68 | 76 | 64 | 30]
--------------------------------------------------------------------
Please enter an integer k, 1<=k<=10, or 'R' to refill the array:
5
在这个数组中,有两个“30”整数,我猜该程序由于某种原因不会移动到下一个整数。
感谢您提供的任何帮助!
【问题讨论】:
-
未引用
partition的last参数。 -
你期望上一个例子的输出是什么?
-
@hgminh 输出应该是第 5 个最小的数字。我相信应该是 64。