传送门 The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8690 Accepted: 2847 Description Newman likes playing with cats. He possesses lots of cats in his home. Because the number of cats is really huge, Newman wants
(找第k大的数) 给定一个长度为1,000,000的无序正整数序列,以及另一个数n(1<=n<=1000000),接下来以类似快速排序的方法找到序列中第n大的数(关于第n大的数:例如序列{1,2,3,4,5,6}中第3大的数是4). #include <iostream> using namespace std; int a[1000001],n,ans = -1; void swap(int &a,int &b) { int c; c = a; a = b; b
(转载请注明出处:http://blog.csdn.net/buptgshengod) 题目介绍 在n个数中取第k大的数(基础篇),之所以叫基础篇是因为还有很多更高级的算法,这些以后再讨论.本文用两种最基本的方法来解决这个问题.使用java语言描述.例子是十个数中取第三大的. 算法一 用冒泡法将n个数从大到小排序,再取第k大. public class test { public static void main(String []args) {
求一组N个数中的第k个最大者,设k=N/2. import java.util.Random; public class K_Max { /** * @param args */ //求第K大的数,保证K大于等于1,小于等于array.length/2哦 public static int TopK(int array[],int K) { int topk[] = new int [K]; for(int i = 0; i<topk.length;i++) topk[i] = array[i]
快速排序 下面是之前实现过的快速排序的代码. function quickSort(a,left,right){ if(left==right)return; let key=partition(a,left,right);//选出key下标 if(left<key){ quickSort(a,left,key-1);//对key的左半部分排序 } if(key<right){ quickSort(a,key+1,right)//对key的右半部份排序 } } function partiti