堆排序 && 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, ...
随机推荐
- pyqt5-动画组QAnimationGroup
from PyQt5.QtWidgets import QApplication, QWidget,QPushButton,QLabel import sys from PyQt5.QtCore im ...
- spark读取kafka数据 createStream和createDirectStream的区别
1.KafkaUtils.createDstream 构造函数为KafkaUtils.createDstream(ssc, [zk], [consumer group id], [per-topic, ...
- c++string int转化简单写法
#include<iostream> #include<string> #include<sstream> //定义了stringstream类 using nam ...
- html canvas标签 语法
html canvas标签 语法 canvas是什么意思? 作用:定义图形,比如图表和其他图像. 说明:<canvas> 标签只是图形容器,通过脚本 (通常是JavaScript)来完成, ...
- Codeforces 912D Fishs ( 贪心 && 概率期望 && 优先队列 )
题意 : 给出一个 N * M 的网格,然后给你 K 条鱼给你放置,现有规格为 r * r 的渔网,问你如果渔网随意放置去捕捞小鱼的情况下,捕到的最大期望值是多少? 分析 : 有一个很直观的想法就是 ...
- ubuntu下,VSCode采用cmake编译C++工程
首先在VSCode中下载CMake和CMake Tools两个插件. 选中CMake Tools,可以看到在VSCode中如何使用cmake编译C++工程的教程. 官网教程 最重要且最实用,看这个网址 ...
- 在一个tomcat中配置多个tomcat服务器 111
<Service name="Tomcat_1"> <Connector port="8888" protocol="HTT ...
- Xdebug bad Zend API Version Number
I am having trouble upgrading xdebug for MAMP. I was running version 2.2.0 and there was a known iss ...
- tp32-layuicms项目介绍
项目结构: 项目截图: 登录页 文章列表 码云仓库:https://gitee.com/lim2018/tp32-layuicms
- Servlet开发详讲
一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...