堆排序做的,没有全部排序,找到第k个就结束

public int findKthLargest(int[] nums, int k) {
int num = 0;
if (nums.length <= 1)
return nums[0];
int heapSize = nums.length;
//1.构建最大堆
int half = (heapSize-2)/2;
for (int i = half;i >= 0;i--)
{
adjust(nums,heapSize,i);
}
while (heapSize > 1)
{
//2.取出最大值
swap(0,heapSize-1,nums);
heapSize--;
num++;
if(num == k)
break;
//3.调整堆
adjust(nums,heapSize,0);
}
return nums[nums.length-k];
} public void adjust(int[] nums,int heapSize,int index)
{
int left = index*2+1;
int right = index*2+2;
int biggest = index;
if (left < heapSize && nums[left] > nums[biggest])
biggest = left;
//注意这里都是和nums[biggest]比较,别写成index,因为要找出最大的值
if (right < heapSize && nums[right] > nums[biggest])
biggest = right;
if (biggest != index)
{
swap(index,biggest,nums);
adjust(nums,heapSize,biggest);
}
}
public void swap(int a,int b,int[] nums)
{
int temp = nums[a];
nums[a] = nums[b];
nums[b] = temp;
}

堆排序的算法时间复杂度是:O(n*log k )

下边快排的改进做法,O(n)

利用快速排序的思想,从数组S中随机找出一个元素X,把数组分为两部分Sa和Sb。Sa中的元素大于等于X,Sb中元素小于X。这时有两种情况:

           1. Sa中元素的个数小于k,则Sb中的第k-|Sa|个元素即为第k大数;
           2. Sa中元素的个数大于等于k,则返回Sa中的第k大数。时间复杂度近似为O(n)
public void findk(int[] nums,int sta,int end,int k)
{
if (sta>end) return;
Random random = new Random();
int p = random.nextInt(end-sta)+sta;
swap(nums,p,sta);
int l = sta;
int r = end;
while (l<r)
{
while (l<r&&nums[r]>=nums[sta])
r--;
while (l<r&&nums[l]<=nums[sta])
l++;
swap(nums,l,r); }
swap(nums,l,sta);
//记录右边数组(包括排序好的数)的大小
int size = end-l+1;
if (size == k) System.out.println(nums[l]);
else if (size<k) findk(nums,sta,l-1,k-size);
else findk(nums,l,end,k);
}
public void swap(int[] nums,int i ,int j)
{
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}

215. Kth Largest Element in an Array找出数组中第k大的值的更多相关文章

  1. 前端算法题:找出数组中第k大的数字出现多少次

    题目:给定一个一维数组,如[1,2,4,4,3,5],找出数组中第k大的数字出现多少次. 例如:第2大的数是4,出现2次,最后输出 4,2 function getNum(arr, k){ // 数组 ...

  2. 【LeetCode】215. Kth Largest Element in an Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:移除最大值 方法二:排序 方法三:大顶堆 方 ...

  3. 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

    传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...

  4. leetcode 215. Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  5. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  6. LN : leetcode 215 Kth Largest Element in an Array

    lc 215 Kth Largest Element in an Array 215 Kth Largest Element in an Array Find the kth largest elem ...

  7. LeetCode OJ 215. Kth Largest Element in an Array 堆排序求解

    题目链接:https://leetcode.com/problems/kth-largest-element-in-an-array/ 215. Kth Largest Element in an A ...

  8. [LeetCode] 215. Kth Largest Element in an Array 数组中第k大的数字

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  9. 【LeetCode】215. Kth Largest Element in an Array (2 solutions)

    Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...

随机推荐

  1. JVM 堆中对象分配、布局和访问

    本文摘自深入理解 Java 虚拟机第三版 对象的创建 Java 是一门面向对象的语言,Java 程序运行过程中无时无刻都有对象被创建出来.从语言层面看,创建对象只是一个 new 关键字而已,而在虚拟机 ...

  2. PriorityQueue 优先队列的实现

    PriorityQueue 的 implementation PriorityQueue即是优先队列.通俗的说就是体育课的时候老师要求从高到低排序,老师能直接一眼看出谁是最高的在班级里.当这个最高的离 ...

  3. springmvc<二> 一些配置

    1.1.3. Web MVC Config    1.1.2中的解析器可以自定义实现,DispatcherServlet检查每个特殊bean的WebApplicationContext,如果没有匹配的 ...

  4. moviepy音视频开发:音频剪辑基类AudioClip

    ☞ ░ 前往老猿Python博文目录 ░ 一.背景知识介绍 1.1.声音三要素: 音调:人耳对声音高低的感觉称为音调(也叫音频).音调主要与声波的频率有关.声波的频率高,则音调也高. 音量:也就是响度 ...

  5. 老猿学5G:3GPP 5G规范中的URI资源概念

    ☞ ░ 前往老猿Python博文目录 ░ 说明: 本文参考3GPP29.501<Principles and Guidelines for Services Definition>结合笔者 ...

  6. 转:Python常见字符编码及其之间的转换

    参考:Python常见字符编码 + Python常见字符编码间的转换 一.Python常见字符编码 字符编码的常用种类介绍 第一种:ASCII码 ASCII(American Standard Cod ...

  7. PyQt(Python+Qt)学习随笔:QTableWidget项编辑方法editItem、openPersistentEditor

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 1.触发编辑项的editItem方法 QTableWidget提供了触发项编辑的方法,调用语法如下: ...

  8. PyQt(Python+Qt)学习随笔:QListView的isWrapping属性

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QListView的isWrapping属性用于控制视图中的数据项项布局在可见区域中没有足够空间时是 ...

  9. Android的intent

    title: Android基础01 date: 2020-02-15 17:17:04 tags: 1.Intent Intent可以让活动进行跳转.使用方式有两种,一种是显式,另一种是隐式. 1. ...

  10. linux服务器性能分析只需1分钟

    背景: 现在的互联网公司,大多数时候应用服务都是部署在linux服务器上,那么当你的服务运行过程中出现了一些响应慢,资源瓶颈等疑似性能问题时,给你60秒,如何快速完成初步检测? 肯定有人会说用工具,公 ...