2018.4.24 快排查找第K大
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大的更多相关文章
- 快排查找第K小的数
#include "iostream.h" using namespace std; int findMedian(int *A,int left,int right){ int ...
- 快排找第k大模板
int get_kth(int l,int r) { if (l==r) return a[r]; ]; while (i<j) { while (a[i]<mid) i++; while ...
- 如何用快排思想在O(n)内查找第K大元素--极客时间王争《数据结构和算法之美》
前言 半年前在极客时间订阅了王争的<数据结构和算法之美>,现在决定认真去看看.看到如何用快排思想在O(n)内查找第K大元素这一章节时发现王争对归并和快排的理解非常透彻,讲得也非常好,所以想 ...
- 基于快速排序思想partition查找第K大的数或者第K小的数。
快速排序 下面是之前实现过的快速排序的代码. function quickSort(a,left,right){ if(left==right)return; let key=partition(a, ...
- 牛客网-3 网易编程题(1拓扑&2二叉树的公共最近祖先&3快排找第K大数)
1. 小明陪小红去看钻石,他们从一堆钻石中随机抽取两颗并比较她们的重量.这些钻石的重量各不相同.在他们们比较了一段时间后,它们看中了两颗钻石g1和g2.现在请你根据之前比较的信息判断这两颗钻石的哪颗更 ...
- 查找第K大的数
类快排 第一种方法 o(n) #include <bits/stdc++.h> using namespace std; const int N = 1000; int s[N]; int ...
- POJ 2985 The k-th Largest Group(树状数组 并查集/查找第k大的数)
传送门 The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8690 Acce ...
- 查找第K大的值
这种题一般是给定N个数,然后N个数之间通过某种计算得到了新的数列,求这新的数列的第K大的值 POJ3579 题意: 用$N$个数的序列$x[i]$,生成一个新序列$b$. 新的序列定义为:对于任意的$ ...
- 820复试算法 快排找第 k 小
done {20-01-30 12:56} ref: https://blog.csdn.net/fengsigaoju/article/details/50728588 note: void qui ...
随机推荐
- shell编程(一)之变量
变量:命名的内存空间 bash的变量种类: 根据变量的生效范围等标准 本地变量: 生效范围为当前shell进程:对当前shell之外的其他shell进程,包括当前shell的子shell进程均无效 环 ...
- jQuery validator plugin之Validator
Validator.destroy() Destroys this instance of validator freeing up resources and unregistering event ...
- Lintcode12-Min Stack-Easy
2. Min Stack Implement a stack with following functions: push(val) push val into the stack pop() pop ...
- 5_bash
bash及其特性:shell:外壳.用户直接接入计算机的时候所使用的外壳程序linux允许一个用户账户登录多次,而这多次登录的每一个打开的shell都是独立的互不相干的shell,它们是三个进程,每一 ...
- FastAdmin笔记~
在phpstud命令行输入以下命令! 1.在命令行一键生成test表格的crud php think crud -t test 2.在数据库创建表必须加上表注释 3.生成test控制器的权限菜单 p ...
- 理解AJAX的原理
1.原生ajax异步请求(ajax的原理) (异步请求:无跳转,无刷新....)通过XMLHttpRequst对象,向服务器发送请求.XMLHttpRequest对象具有一些属性和方法. 1.首先创建 ...
- js 字符串,new String() 与 String()
function showCase(value) { switch(value) { case 'A': console.log('Case A'); break; case 'B': console ...
- js 可以表示的最大值
, ); ; ; for (var i = START; i <= END; i++) { count++; } console.log(count); // A. 0 // B. 100 // ...
- robot framework测试数据语法
Robot Framework通过文件的扩展名来选择使用何种解析器. 扩展名不分大小写. 可以识别的扩展名包括: HTML: .html, .htm 和 .xhtml TSV: .tsv 纯文本: . ...
- 【虚拟机】解决网络适配器没有 VirtualBox Host-Only Ethernet Adapter 问题
下面以windows系统来演示重新安装 VirtualBox Host-Only Ethernet Adapter的方法 1.“win+r”输入“devmgmt.msc”,出现如下界面: 2.点击菜单 ...