堆排序 && 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, ...
随机推荐
- 【BZOJ1999】树网的核
题目大意:题目过长,无法简单描述... 题解: 由于树网的核一定是树直径的一段,因此考虑先将直径取出,通过两次 BFS 即可.要求的东西是树上任意一点到这条取出的线段的距离的最大值,发现这个最大值有可 ...
- 数据可视化--> numpy
一.NumPy 1.简介: 官网链接:http://www.numpy.org/ NumPy是Python语言的一个扩充程序库.支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库 ...
- ESP8266-Station模式--我想连上谁
Station模式又叫做站点工作模式,类似于无线终端 处于Station模式下的ESP8266,可以连接到AP.通过Station(简称为“STA”)模式,ESP8266作为客户端连接到路由的wifi ...
- Python条件控制与循环
条件控制语句:if 循环语句:while.for 其他语句:continue.break.pass 1.if语句 # ================================ a = 1 if ...
- python中的文件读取
---恢复内容开始--- r模式,只读模式,不可写入,文件不存在会报错 #r模式,能读不能写,文件不存在会报错 f = open('a1.txt')#不写'r',默认只读 result = f.rea ...
- 分组统计 over(partition by
sum( CASE WHEN ISNULL(b.zl, 0) = 0 THEN C.LLZL ELSE b.zl END * c.pccd * b.sl) over(partition by b.dj ...
- Linux基础教程 linux下cat 命令使用详解
cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 1.命令格式: cat [选项] [文件] ...
- Linux培训教程 Git在linux下的使用
*初始化git仓库,使用gitinit命令 *添加文件到git仓库分两步: 1.使用git add filename ;可分多次使用,添加多个文件到暂存区 2.使用git commit -m “ ...
- Codeforces 1203F1 Complete the Projects (easy version)
cf题面 Time limit 2000 ms Memory limit 262144 kB 解题思路 看见这题觉得贪心可做,那就贪吧.(昨天真是贪心的一天,凌晨才被这两道贪心题虐,下午多校又来,感觉 ...
- python常用类库总结
个人学习总结,如有错误,请留言指正. 类库归类总结 类库关系