今天我写这个算法的时候发现了很多运行的时候总是有很多问题,先挂上代码:
package org.iaiai.suanfa;
/**
*
* <p>
* Title: BFPRT.java
* </p>
* <p>
* E-Mail: 176291935@qq.com
* </p>
* <p>
* QQ: 176291935
* </p>
* <p>
* Http: iaiai.iteye.com
* </p>
* <p>
* Create time: 2011-8-5
* </p>
*
* @author 丸子
* @version 0.0.1
*/
public class BFPRT {
public static final int N = 5;
public static int number[];
public BFPRT(int size) {
number = new int[size];
System.out.println("the arrray is:");
for (int i = 0; i < size; i++) {
number[i] = (int) (Math.random() * 12);
System.out.print(number[i] + ",");
}
System.out.println("");
}
public void sw(int i, int j) {
int tmp = number[i];
number[i] = number[j];
number[j] = tmp;
/*
* number[i]^=number[j]; number[j]^=number[i]; number[i]^=number[j];
*/
}
public void bubleSort(int begin, int end) { // /元素不包括end的
for (int i = end - 1; i > 0; i--) {
for (int j = begin; j < i; j++) {
if (number[j] > number[j + 1]) {
sw(j, j + 1);
}
}
}
}
public int partion(int begin, int end, int NUM) {// 将小于NUM的放在左边,将大于等于NUM的放在右边
int pbegin = begin;
int pend = end - 1;
System.out.println("the NUM:" + NUM);
while ((pbegin <= pend)) {
System.out.println("pbegin=" + pbegin);
System.out.println("number[pbegin]=" + number[pbegin]);
System.out.println("number[pbegin]>=NUM: "
+ (number[pbegin] >= NUM));
if ((number[pbegin] >= NUM)) {
while ((number[pend] >= NUM)) {
System.out.println("pend=" + pend);
pend--;
}
if (pbegin > pend) { // 这里要判断是否越界了
return pbegin;
}
sw(pbegin, pend);
System.out.println("pend==" + pend + " and pbegin=" + pbegin);
pend--;
}
pbegin++;
}
return pbegin;
}
public void printARR(int begin, int end) {
System.out.println("the array is:");
for (int i = begin; i < end; i++) {
System.out.print(number[i] + ",");
}
System.out.print("\n");
}
public int selectKnum(int begin, int end, int K) { // 寻找第K小的
// printARR(begin,end);
int i = 0;
int size = end - begin;
if (size <= 9) {
if (K > size) {
System.out
.println("the K is:" + K + " and the size is:" + size);
System.out.println("the K is overall the numbers length");
return -1;
}
bubleSort(begin, end);
return number[begin + K - 1];
}
for (i = 0; i < (end - begin) / N; i++) {
bubleSort(i * N + begin, (i + 1) * N + begin);
sw(begin + i, i * N + 2 + begin); // 将每一组中的中位数放到数组的前面去
}
printARR(begin, end);
System.out.println("the i is:" + i);
int x = selectKnum(begin, begin + i, (end - begin + 6) / 10);
int Pnow = partion(begin, end, x);
int j = Pnow - begin;
printARR(begin, end);
System.out.println("the x is:" + x);
System.out.println("the pNow is:" + Pnow);
System.out.println("the j is:" + j);
System.out.println("the K is: " + K);
if (K > j) {
return selectKnum(Pnow, end, K - j);
} else {
return selectKnum(begin, Pnow, K);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int size = 15;
int nth = 14;
BFPRT A = new BFPRT(size);
int num = A.selectKnum(0, size, nth);
System.out.println("the " + nth + "'num is:" + num);
}
}
运行后直接爆栈!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
引用
the array is:
2,3,2,7,8,2,2,2,3,9,8,
the x is:2
the pNow is:4
the j is:0
the K is: 10
the array is:
3,2,2,7,8,2,2,2,3,9,8,
the i is:2
the NUM:2
pbegin=4
number[pbegin]=2
number[pbegin]>=NUM: true
pend=14
pend=13
pend=12
pend=11
pend=10
pend=9
pend=8
pend=7
pend=6
pend=5
pend=4
the array is:
2,3,2,7,8,2,2,2,3,9,8,
the x is:2
the pNow is:4
the j is:0
the K is: 10
the array is:
3,2,2,7,8,2,2,2,3,9,8,
the i is:2
the NUM:2
pbegin=4
number[pbegin]=2
number[pbegin]>=NUM: true
pend=14
pend=13
pend=12
pend=11
pend=10
pend=9
pend=8
pend=7
pend=6
pend=5
pend=4
the array is:
2,3,2,7,8,2,2,2,3,9,8,
the x is:2
the pNow is:4
the j is:0
the K is: 10
the array is:
3,2,2,7,8,2,2,2,3,9,8,
the i is:2
the NUM:2
pbegin=4
number[pbegin]=2
number[pbegin]>=NUM: true
pend=14
pend=13
pend=12
pend=11
pend=10
pend=9
pend=8
pend=7
pend=6
pend=5
pend=4
the array is:
2,3,2,7,8,2,2,2,3,9,8,
the x is:2
the pNow is:4
the j is:0
the K is: 10
the array is:
3,2,2,7,8,2,2,2,3,9,8,
the i is:2
the NUM:2
pbegin=4
number[pbegin]=2
number[pbegin]>=NUM: true
pend=14
pend=13
pend=12
pend=11
pend=10
pend=9
pend=8
pend=7
pend=6
pend=5
pend=4
the array is:
2,3,2,7,8,2,2,2,3,9,8,
the x is:2
the pNow is:4
the j is:0
the K is: 10
Exception in thread "main" java.lang.StackOverflowError
at sun.nio.cs.ext.GB18030$Encoder.encodeArrayLoop(Unknown Source)
at sun.nio.cs.ext.GB18030$Encoder.encodeLoop(Unknown Source)
at java.nio.charset.CharsetEncoder.encode(Unknown Source)
at sun.nio.cs.StreamEncoder.implWrite(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.BufferedWriter.flushBuffer(Unknown Source)
at java.io.PrintStream.write(Unknown Source)
at java.io.PrintStream.print(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at BFPRT.printARR(BFPRT.java:59)
at BFPRT.selectKnum(BFPRT.java:83)
at BFPRT.selectKnum(BFPRT.java:94)
at BFPRT.selectKnum(BFPRT.java:94)
看到没有,总是在两个数组之间不停的切换,原因是在数字有相同的情况下,可能存在一种情况就是那样的,select与partion不停的切换,而且此时select找到的中位数在第一位,后来的partion找到把数组还原了。
所以我的程序是有BUG的,如何消除由相同数的影响,你可以采用重新随机打乱来操做(但是还是可能会出现那样的情况,比如全部是2),
所以这样地方法适用在所有数字互不相同的情况下面。切记这个话,所以这个算法不是万能的,要分情况使用;
分享到:
相关推荐
"期望值是O(n)代价" 指出所使用的算法在平均情况下运行时间复杂度为线性,而 "最坏情况为O(n)BFPRT算法" 提及了BFPRT算法,这是一种在最坏情况下也能保持线性时间复杂度的高效算法。 首先,让我们深入了解一下第k小...
BFPRT算法,全称为Blum-Floyd-Pratt-Rivest-Tarjan算法,是由Manuel Blum、Robert W. Floyd、Vaughan Pratt、Ronald L. Rivest和Robert E. Tarjan五位计算机科学家在1973年的《计算机与系统科学杂志》上提出的。该...
以下是28个必须知道的算法中的一部分,包括了并查集、KMP算法、BFPRT算法、快速排序、Floyd-Warshall最短路径算法、Gentry的完全同态加密、深度优先搜索和广度优先搜索、Miller-Rabin素数测试、二分查找以及霍夫曼...
BFPRT算法解决的问题十分经典,即从某n个元素的序列中选出第k大(第k小)的元素,通过巧妙的分析,BFPRT可以保证在最坏情况下仍为线性时间复杂度。该算法的思想与快速排序思想相似,为使得算法在最坏情况下,依然能...
BFPRT算法,也称为中位数的中位数选择算法,是由Blum、Floyd、Pratt、Rivest和Tarjan共同开发的。它能在线性时间内找到数组中的第k小元素,通过选取中位数的中位数作为枢轴来保证算法的高效性。相比于快速排序,...
由Blum等人提出的BFPRT算法,用于在未排序的数组中找到第k小的元素,平均时间复杂度为O(N),通过巧妙地选取枢轴元素以避免最坏情况的发生。 9. **Knuth-Morris-Pratt(KMP)字符串匹配算法** KMP算法是一种高效的...
2. **查找中位数的中位数:**递归地使用BFPRT算法查找上一步骤中所有中位数的中位数。 3. **分割与递归:**根据中位数的中位数将序列分为两部分,并根据目标k的值决定在哪一部分继续查找。 **应用场景:** - BFPRT...
BFPRT算法是用于在数组中找到第k大元素的线性时间复杂度算法。通过精心设计的枢轴选取策略,它能保证在最坏情况下达到线性时间复杂度,优于传统的基于比较的排序算法。 4. **快速排序(Quicksort)**: 快速排序是...
BFPRT算法用于在一个序列中找出第k大的(或第k小的)元素,即使在最坏情况下也能保持线性时间复杂度。它通过分组、找出中位数、递归查找中位数的中位数以及利用中位数分割数组等步骤来高效地定位目标元素。 6. **...
BFPRT算法是一种用于寻找序列中第k大(或第k小)元素的算法,由Blum等五位学者共同提出。其核心思想与快速排序类似,但通过巧妙的设计确保了最坏情况下的线性时间复杂度。BFPRT算法的主要步骤包括: 1. **分组**:...
3. **BFPRT算法(中位数之中位数算法)**: 这是由五位著名科学家共同提出的在数组中找到第k大元素的线性时间复杂度算法。通过精心设计的枢轴选取策略,确保最坏情况下仍保持线性效率,优于传统的基于比较的排序算法。 ...
BFPRT算法是由Blum等人于1973年提出的,用于在数组中找到第k小的元素。该算法通过选择合适的枢轴元素来划分数组,并确保枢轴左边的元素都不大于它,右边的元素都不小于它。这种方法能够在平均情况下达到线性时间...
3. **BFPRT 算法**:也称为“中位数之中位数算法”,由多位著名计算机科学家共同提出,用于在数组中寻找第k大元素,确保在最坏情况下仍能保持线性时间复杂度。 4. **快速排序 (Quicksort)**:由C.A.R. Hoare提出的...
【描述】:"目录经典算法Manacher算法原始问题进阶问题BFPRT算法morris遍历二叉树遍历规则先序、中序序列后序序列时间复杂度分析求和为aim的最长子数组长度" 在算法面试中,掌握基础和进阶算法是至关重要的。本篇...
8. **BFPRT算法**:又称为“中位数的中位数”算法,用于在未排序数组中找到第k小的元素,平均时间复杂度为O(n),是快速选择算法的一个变体,避免了最坏情况下的性能下降。 9. **布隆过滤器 (Bloom Filter)**:虽然...
BFPRT算法用于寻找一个序列中的第 k 大元素,具有线性时间复杂度。 **特点:** - 保证在最坏情况下的线性时间复杂度。 - 类似于快速排序,但采用了更精细的划分策略以避免最坏情况的发生。 **算法步骤:** 1. **...
此外,为减少中值处理时间,论文还应用了BFPRT算法。 第三,根据沙氏大气激光雷达的工作原理和测量流程,设计了相应的控制软件。软件实现了像素到距离的校准功能,支持多通道探测,确保数据的精确性。借助...
主要处理边缘情况时间/空间复杂度分析专题排序链表题LRU 二分位运算栈和含量堆二叉树贪心并查集图动态规划单调栈滑动窗口...bfprt算法线段树和树状斑点打表矩阵处理技巧斑点二维坐标卡特兰数约瑟夫环问题股票问题[TODO...
Blum-Floyd-Pratt-Rivest-Tarjan (BFprt)算法就是基于这种思想,其时间复杂度为线性\(O(n)\)。 ##### 6. 计算几何问题 在计算几何领域,分而治之策略可以用来解决诸如找出二维空间中距离最近的两个点等问题。该...