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. 事件Event实现消费者模型

    import timeimport randomfrom multiprocessing import Process,Queue def consumer(q,name): while True: ...

  2. luoguP1850 换教室

    luoguP1850 换教室 链接 https://www.luogu.org/problemnew/show/P1850 思路 状态很显然就是f[n][k][0/1] 前i次,用了k次机会,当前是在 ...

  3. centos6.5下安装Nginx

    链接: https://www.jb51.net/article/118595.htm

  4. Yahoo Programming Contest 2019 E - Odd Subrectangles

    E - Odd Subrectangles 思路: 对于行方案固定的情况下,假设和为奇数的列为a个,和为偶数的列为b个,a+b = m 那么从奇数里面选奇数个,即C(a, 1) + C(a, 3) + ...

  5. 【快捷键】IntelliJ IDEA For Mac 常用快捷键

    一.符号对应关系 ⌃ control ⌥ option ⌘ command ⇧ shift 二.常用快捷键 1.control+shift+J 两行整理成一行 2.command+shift+F12 ...

  6. Linux 独立启动方式安装 Archiva

    为了方便起见,我们假设你的 archiva 安装到目录 /opt 下面. 下载安装程序 进入 Archiva 的项目的下载页面中,请单击链接:https://archiva.apache.org/do ...

  7. 『OpenCV3』基于色彩分割图片

    一.遍历图像实现色彩掩码 本节我们实现这样一个算法,我们指定某种颜色和一个阈值,根据输入图片生成一张掩码,标记符合的像素(和指定颜色的差异在阈值容忍内). 源代码如下,我们使用一个class完成这个目 ...

  8. python_入门_三级菜单

    '''程序:三级菜单要求:1.打印省.市.县三级菜单2.可返回上一级3.可随时退出程序''' # -*- coding: utf-8 -*- # __author__ = 'qi' prov_city ...

  9. vue-router beforeEach死循环

    vue中页面跳墙处理 页面跳墙中使用 vue-router中的 beforeEach的死循环问题 问题展现 import Router from 'vue-router' const router = ...

  10. SpringCloud服务负载均衡实现原理02