堆排序 && 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, ...
随机推荐
- HashMap的底层实现以及解决hash值冲突的方式
class HashMap<K,V> extends AbstractMap<K,V> HashMap put() HashMap get() 1.put() HashMa ...
- MongoDB 副本集+分片 认证方式搭建
MongoDB 副本集+分片 认证方式搭建 参考资料: https://www.cnblogs.com/ityouknow/p/7344005.htmlhttps://jorwen-fang.itey ...
- ES和RDBMS区别
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/12090637.html ES7.0之前,一个Index可以设置多个Type ES7.0开始,Type已 ...
- 算法——得到数据流中前K大的数
用优先队列 public PriorityQueue<Integer> kthLargest(int k, int[]a) { PriorityQueue<Integer> q ...
- 【leetcode】Find Largest Value in Each Tree Row
You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...
- Python 元组Ⅱ
删除元组 元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组,如下实例: 以上实例元组被删除后,输出变量会有异常信息,输出如下所示: 元组运算符 与字符串一样,元组之间可以使用 + ...
- Vim 命令、操作、快捷键(收藏大全)
------ 命令历史 以:和/开头的命令都有历史纪录,可以首先键入:或/然后按上下箭头来选择某个历史命令. 启动vim 在命令行窗口中输入以下命令即可 vim 直接启动vim vim filenam ...
- [CSP-S模拟测试]:最小距离(最短路)
题目传送门(内部题97) 输入格式 第一行三个整数$n,m,p$,第二行$p$个整数$x_1\sim x_p$表示特殊点的编号.接下来$m$行每行三个整数$u,v,w$表示一条连接$u$和$v$,长度 ...
- bootstrap editable初始化后表单可修改数据
function loadData() { var url = "${ctx }/sys/marketing/product/page"; $('#tablepager').boo ...
- 14 补充 MySQL的创建用户和授权
权限管理 我们知道我们的最高权限管理者是root用户,它拥有着最高的权限操作.包括select.update.delete.update.grant等操作.那么一般情况在公司之后DBA工程师会创建一个 ...