import java.util.Arrays;

/*
核心思想:利用快排思想,先假定从大到小排序,找枢纽,枢纽会把大小分开它的两边,当枢纽下标等于k时,
即分了k位在它左边或右边,也就是最大或最小的排到了它的左边或右边了。那么那个枢纽就是要找的第k位了
*/
public class SearchNumData {
/*
n为数组长度
k为要查找的第k大
*/
public static int findKth(int[] a, int n, int K) {
return findKth(a, 0, n - 1, K);
} /*
start为数组最低位下标
end为数组最高位下标
*/
public static int findKth(int[] a, int start, int end, int k) {
//先进行一次快排,取得枢纽
int pivot = partation(a, start, end);
//pivot-start+1表示快排的前半段元素的个数(包括中轴)
//每次划分后,大的在左边,小的在右边
if (k == pivot - start + 1){
return a[pivot];
} else if (k > pivot - start + 1) {
//说明第k大的元素在后半段,所以往后面查,start=pivot+1,k-(pivot-start+1)。往后查的还是整个数组的第k大,每次次快排枢纽的时候,已经把大的放右边了。
return findKth(a, pivot + 1, end, k - pivot + start - 1);
} else{
//则第k大的元素在前半段,更新end=pivot-1
return findKth(a, start, pivot - 1, k);
}
}
//快排,找枢纽,从大到小排序
public static int partation(int[] a, int low, int high) {
int key = a[low];
while (low < high) {
while (low < high && a[high] <= key)
high--;
a[low] = a[high];
while (low < high && a[low] >= key)
low++;
a[high] = a[low];
}
a[low] = key;
return low;
} public static void quicksort(int[] a, int low, int high) {
if(low < high) {
int pivot = partation(a, low, high);
quicksort(a, low, pivot-1);
quicksort(a, pivot+1, high);
}
} public static void main(String[] args) {
int[] array = {
9, 1, 5, 3, 5, 2, 6, 8, 7, 6, 9, 8
};
System.out.println("----------原数组-----------");
for(int i=0;i<array.length;i++){
System.out.print(array[i]+" ");
}
System.out.println();
int [] array2 = array.clone();
int k = 4;
int num=findKth(array,array.length,k);
System.out.println("----------第"+k+"大-----------");
System.out.println(num);
System.out.println("----------处理后-----------");
for(int i=0;i<array.length;i++){
System.out.print(array[i]+" ");
}
System.out.println();
System.out.println("----------原数组排序后-------");
// Arrays.sort(array);
quicksort(array,0,array.length-1);
for(int i=0;i<array.length;i++){
System.out.print(array[i]+" ");
}
System.out.println();
System.out.println("------------------------");
for(int i=0;i<array2.length;i++){
if (num==array2[i]){
System.out.println("原数组位置-----"+i);
}
}
}
}

输出:

----------原数组-----------
9 1 5 3 5 2 6 8 7 6 9 8
----------第4大-----------
8
----------处理后-----------
9 9 8 8 7 6 6 5 5 3 2 1
----------原数组排序后-------
9 9 8 8 7 6 6 5 5 3 2 1
------------------------
原数组位置-----7
原数组位置-----11

2018.4.24 快排查找第K大的更多相关文章

  1. 快排查找第K小的数

    #include "iostream.h" using namespace std; int findMedian(int *A,int left,int right){ int ...

  2. 快排找第k大模板

    int get_kth(int l,int r) { if (l==r) return a[r]; ]; while (i<j) { while (a[i]<mid) i++; while ...

  3. 如何用快排思想在O(n)内查找第K大元素--极客时间王争《数据结构和算法之美》

    前言 半年前在极客时间订阅了王争的<数据结构和算法之美>,现在决定认真去看看.看到如何用快排思想在O(n)内查找第K大元素这一章节时发现王争对归并和快排的理解非常透彻,讲得也非常好,所以想 ...

  4. 基于快速排序思想partition查找第K大的数或者第K小的数。

    快速排序 下面是之前实现过的快速排序的代码. function quickSort(a,left,right){ if(left==right)return; let key=partition(a, ...

  5. 牛客网-3 网易编程题(1拓扑&2二叉树的公共最近祖先&3快排找第K大数)

    1. 小明陪小红去看钻石,他们从一堆钻石中随机抽取两颗并比较她们的重量.这些钻石的重量各不相同.在他们们比较了一段时间后,它们看中了两颗钻石g1和g2.现在请你根据之前比较的信息判断这两颗钻石的哪颗更 ...

  6. 查找第K大的数

    类快排 第一种方法 o(n) #include <bits/stdc++.h> using namespace std; const int N = 1000; int s[N]; int ...

  7. POJ 2985 The k-th Largest Group(树状数组 并查集/查找第k大的数)

    传送门 The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8690   Acce ...

  8. 查找第K大的值

    这种题一般是给定N个数,然后N个数之间通过某种计算得到了新的数列,求这新的数列的第K大的值 POJ3579 题意: 用$N$个数的序列$x[i]$,生成一个新序列$b$. 新的序列定义为:对于任意的$ ...

  9. 820复试算法 快排找第 k 小

    done {20-01-30 12:56} ref: https://blog.csdn.net/fengsigaoju/article/details/50728588 note: void qui ...

随机推荐

  1. CF285E Positions in Permutations

    思路 dp+二项式反演的神题 就是dp部分非常麻烦(好吧是我傻了 考虑先钦定m个满足条件的位置,这m个\(x_i\),只能放\(x_i-1\)或\(x_i+1\),然后其他的随便放(得出至少m个的方案 ...

  2. 论文笔记:Improving Deep Visual Representation for Person Re-identification by Global and Local Image-language Association

    Improving Deep Visual Representation for Person Re-identification by Global and Local Image-language ...

  3. 异常:unity3d ArgumentException: The Assembly System.Configuration is referenced by System.Data.

    异常:ArgumentException: The Assembly System.Configuration is referenced by System.Data. But the dll is ...

  4. springboot2.0 最大上传文件大小遇到的错误Failed to bind properties under 'spring.servlet.multipart.max-file-size'

    错误: 解决: 把100Mb改为100MB

  5. R apply函数 三维 array

    参考自:https://www.cnblogs.com/nanhao/p/6674063.html 首先,生成三维数组,注意该三维矩阵为 2*3*4的维度: x=array(1:24,c(2,3,4) ...

  6. nodejs点滴

    1.exports与module.exports http://cnodejs.org/topic/5231a630101e574521e45ef8 因为require指向了module.export ...

  7. eclipse get set 自动添加注释

    编码的时候通常要用到 JavaBean ,而在我们经常把注释写在字段上面,但生成的Get/Set方法不会生成,通过修改Eclipse源码可解决,直接上例子: /** * 员工ID */ private ...

  8. HTML 点击图片放大

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 『TensorFlow』命令行参数解析

    argparse很强大,但是我们未必需要使用这么繁杂的东西,TensorFlow自己封装了一个简化版本的解析方式,实际上是对argparse的封装 脚本化调用tensorflow的标准范式: impo ...

  10. 若依项目分模块集成uflo2

    关于若依分模块创建项目可参考:https://www.cnblogs.com/conswin/p/9766186.html 了解uflo2,uflo2是一套由BSTEK自主研发的基于Java的工作流引 ...