堆排序 && Kth Largest Element in an Array
堆排序
堆节点的访问
通常堆是通过一维数组来实现的。在数组起始位置为0的情形中:
父节点i的左子节点在位置(2*i+1);
父节点i的右子节点在位置(2*i+2);
子节点i的父节点在位置floor((i-1)/2);
堆的操作
在堆的数据结构中,堆中的最大值总是位于根节点(在优先队列中使用堆的话堆中的最小值位于根节点)。堆中定义以下几种操作:
最大堆调整(Max_Heapify):将堆的末端子节点作调整,使得子节点永远小于父节点
创建最大堆(Build_Max_Heap):将堆所有数据重新排序。从最后一个父节点开始调用“最大堆调整”,直到第一个父节点(根节点)
堆排序(HeapSort):移除位在第一个数据的根节点,并做最大堆调整的递归运算
Java
package test;
public class Solution {
private void maxHeapify(int[] nums, int index, int heapSize) {
int iMax, iLeft, iRight;
while(true) {
iMax = index;
iLeft = 2 * index + 1;
iRight = 2 * index + 2;
if (iLeft < heapSize && nums[iMax] < nums[iLeft]) {
iMax = iLeft;
}
if (iRight < heapSize && nums[iMax] < nums[iRight]) {
iMax = iRight;
}
if(iMax != index) {
int tmp = nums[iMax];
nums[iMax] = nums[index];
nums[index] = tmp;
index = iMax;
} else {
break;
}
}
}
private void buildMaxHeap(int[] nums) {
int lastParent = (int) Math.floor((nums.length-1) / 2);
for(int i=lastParent; i>=0; i--) {
maxHeapify(nums, i, nums.length);
}
}
public void heapSort(int[] nums) {
buildMaxHeap(nums);
for(int i=nums.length-1; i>=0; i--) {
int tmp = nums[0];
nums[0] = nums[i];
nums[i] = tmp;
maxHeapify(nums, 0, i);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] nums = {3,5,2,1,0,9,4,5,6,7,3,2,6};
Solution s = new Solution();
s.heapSort(nums);
for(int num: nums) {
System.out.print(num + " ");
}
}
}
参考
https://zh.wikipedia.org/wiki/堆排序
http://bubkoo.com/2014/01/14/sort-algorithm/heap-sort/
Kth Largest Element in an Array
来源:https://leetcode.com/problems/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 sorted order, not the kth distinct element.
For example,
Given [3,2,1,5,6,4] and k = 2, return 5.
Note:
You may assume k is always valid, 1 ≤ k ≤ array's length.
利用堆排序思想,创建最大堆,重复“移除位在第一个数据的根节点,并做最大堆调整的递归运算”这一步骤,第K次移除的根节点就是数据中第K大的元素。
也可利用快速排序的思想,见http://www.cnblogs.com/renzongxian/p/7465453.html
Java
class Solution {
private void maxHeapify(int[] nums, int index, int heapSize) {
int iMax, iLeft, iRight;
while(true) {
iMax = index;
iLeft = 2 * index + 1;
iRight = 2 * index + 2;
if(iLeft < heapSize && nums[iMax] < nums[iLeft]) {
iMax = iLeft;
}
if(iRight < heapSize && nums[iMax] < nums[iRight]) {
iMax = iRight;
}
if(iMax != index) {
int tmp = nums[iMax];
nums[iMax] = nums[index];
nums[index] = tmp;
index = iMax;
} else {
break;
}
}
}
private void buildMaxHeap(int[] nums) {
int lastParent = (int)Math.floor((nums.length-1) / 2);
for(int i=lastParent; i>=0; i--) {
maxHeapify(nums, i, nums.length);
}
}
public int findKthLargest(int[] nums, int k) {
buildMaxHeap(nums);
for(int i=nums.length-1; i>=nums.length-k; i--) {
int tmp = nums[0];
nums[0] = nums[i];
nums[i] = tmp;
maxHeapify(nums, 0, i);
}
return nums[nums.length-k];
}
} // 7 ms
堆排序 && Kth Largest Element in an Array的更多相关文章
- 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 ...
- [Leetcode Week11]Kth Largest Element in an Array
Kth Largest Element in an Array 题解 题目来源:https://leetcode.com/problems/kth-largest-element-in-an-arra ...
- Kth Largest Element in an Array
Find K-th largest element in an array. Notice You can swap elements in the array Example In array [9 ...
- leetcode面试准备:Kth Largest Element in an Array
leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...
- 【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 ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- Leetcode 之 Kth Largest Element in an Array
636.Kth Largest Element in an Array 1.Problem Find the kth largest element in an unsorted array. Not ...
- Lettcode Kth Largest Element in an Array
Lettcode Kth Largest Element in an Array 题意:在无序数组中,寻找第k大的数字,注意这里考虑是重复的. 一直只会简单的O(nlogn)的做法,听说这题有O(n) ...
- 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array
传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...
随机推荐
- IDEA部署Spring-boot到Docker容器
一.准备工作 idea安装Docker插件 centos7系统安装docker 二.打开docker远程端口 1. 编辑docker.service文件 vim /usr/lib/systemd/sy ...
- 一种循环C字符数组的骚操作
#include <stdio.h> #include <stdlib.h> int main() { char wenwa[] = "程劲小盆友在做什么" ...
- float浮动导致父元素高度坍塌的原因及清除浮动方法
一.浮动产生原因 一般浮动是什么情况呢?一般是一个盒子里使用了CSS float浮动属性,导致父级对象盒子不能被撑开,这样CSS float浮动就产生了. 本来两个黑色对象盒子是在红色盒子内, ...
- DB2的HADR
db2stop force; db2start; db2 restore db clmsdb; db2 start hadr on db clmsdb as standby; ************ ...
- C++ std::vector 总结笔记
Initialization #include<iostream> #include<vector> using namespace std; int main() { vec ...
- redis集群搭建_超详细
redis集群中至少应该有三个节点,以保证当集群中的某个节点挂掉,其他节点进行容错投票时,投票数能超过半票. 要保证高可用,则还需要每一个节点有一个备份机. 因此redis集群至少需要6台服务器.这里 ...
- ASP教程:gb2312和utf-8乱码问题解决
文章为转载:https://www.xp.cn/b.php/31663.html 尊重作者版权 今天做网站的时候,客户要一个博客,于是就利用了本博客所用的程序pjblog.经常做网站的人都知道,在同一 ...
- js for循环 框架内部的
var head001 =true; var head002 = true; var head003 = true; ; h++) { console.log(h); } h=; ;h < ; ...
- cocos2d 15款游戏源码
https://blog.csdn.net/jailman/article/details/78678972
- Vue项目开发,nprogress进度条加载之超详细讲解及实战案例
Nprogress的默认进度条很细,它的设计灵感主要来源于 谷歌,YouTube 他的安装方式也很简单,你可以有两种使用方式: 直接引入js和css文件 使用npm安装的的方式 直接引入: Npm安装 ...