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 ...
随机推荐
- 分析hello1项目里面的web.xml
在example目录下的web\jsf\hello1\target\hello1\WEB-INF路径里可以找到hello1的web.xml <?xml version="1.0&quo ...
- HAOI(多省联考)2019退役记
等着回头写 算了今天先写点 Day -1 打扫下机房,不想写题,不想考试.... Day 0 上午颓了一上午 下午看下考场结果去早了 ZYZ 全员进队! Day 1 上来T1,01Tire!,开码,半 ...
- Java 设计模式学习笔记1——策略模式(Duck例子)
0.假设现有工程(Duck)中遇到为类添加功能的问题,如何设计类添加新的功能? 1.利用继承提供的Duck(鸭子)的行为会导致哪些缺点? (1)代码在多个子类中重复 (2)很多男知道所有鸭子的全部行为 ...
- Promise使用
Promise可以进行异步操作,比起回调函数,更加容易维护. 首先创建一个简单的Promise var p = new Promise( () => {}); console.log(p); / ...
- Kayleigh O'Connor - I Won't Be There
Do you feel like you're about to drown The wave is rushing over you throw you onto now I remember th ...
- three.js 第二篇:场景 相机 渲染器 物体之间的关系
w我用画画来形容他们之间的关系 场景就是纸张 相机就是我们的眼睛 物体就是在我们脑海中构思的那个画面 渲染器就是绘画这个动作 场景(Scene): 初始化:var scene = new THREE. ...
- Introduce oneself
首先,我是一个男生, 我很喜欢打游戏,钟爱LOL,接触它已经7年了.虽然还是很菜,但就是喜欢.选择计算机科学与技术这个专业呢,就是因为喜欢电脑,可以和室友一起开黑,然而室友都不玩,有点难受. 此外呢, ...
- icpc2018-焦作-D-几何模拟
https://nanti.jisuanke.com/t/34142 上午可能是供氧不足,推的式子死活不对,晚上莫名其妙又来了一次就过了. 分两种情况讨论,如果能够完全进入弯道答案就是固定的就是: s ...
- .NET学习日记【1】
不得不说,之前一年学习的内容基本上在第一章中都有所涉及,而且还讲了很多不知道知识.看完第一张对多态和继承都多了一些体会.在1.4前面的都有很认证的看过,也在vs上面验证了一下.然后也明白了.NET到底 ...
- LOJ10155数字转换
题目描述 如果一个数 x 的约数和 y (不包括他本身)比他本身小,那么 x 可以变成 y,y 也可以变成 x.例如 4 可以变为 3,1 可以变为 7.限定所有数字变换在不超过 n 的正整数范围内进 ...