堆排序

堆节点的访问

通常堆是通过一维数组来实现的。在数组起始位置为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的更多相关文章

  1. 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 ...

  2. [Leetcode Week11]Kth Largest Element in an Array

    Kth Largest Element in an Array 题解 题目来源:https://leetcode.com/problems/kth-largest-element-in-an-arra ...

  3. 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 ...

  4. leetcode面试准备:Kth Largest Element in an Array

    leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...

  5. 【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 ...

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

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

  7. 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 ...

  8. Lettcode Kth Largest Element in an Array

    Lettcode Kth Largest Element in an Array 题意:在无序数组中,寻找第k大的数字,注意这里考虑是重复的. 一直只会简单的O(nlogn)的做法,听说这题有O(n) ...

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

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

随机推荐

  1. 【NOIP2016提高A组五校联考2】running

    题目 小胡同学是个热爱运动的好孩子. 每天晚上,小胡都会去操场上跑步,学校的操场可以看成一个由n个格子排成的一个环形,格子按照顺时针顺序从0 到n- 1 标号. 小胡观察到有m 个同学在跑步,最开始每 ...

  2. 【leetcode database】Human Traffic of Stadium

    X city built a new stadium, each day many people visit it and the stats are saved as these columns: ...

  3. Django 的 CBV&FBV

    Django FBV, function base view  视图里使用函数处理请求 url 1 url(r‘^users/‘, views.users), views 1 2 3 4 5 from ...

  4. webUploader---实现大文件断点续传

    核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...

  5. ali之mtl平台学习

    摩天轮平台可以进行无线测试.设备借用.打包发布.线上监控等功能. 无线测试包括:mock测试.真机适配.代码审查.验收报告等. mock测试类似于fiddler,主要用于接口查看,可以查看请求,返回串 ...

  6. 关于MySQL去除查询结果重复值

    下面先来看看例子: table:  id name  1 a  2 b  3 c  4 c  5 b 库结构大概这样,这只是一个简单的例子,实际情况会复杂得多. 比如我想用一条语句查询得到name不重 ...

  7. 为什么JPA@Modifying需要@Transactional注解

    在JPA开发中遇到一个很奇怪的问题,@Modifying需要和@Transactional配合使用才能正常使用.如下面代码所示 @Modifying @Transactional @Query(&qu ...

  8. controller大全(推荐)

    @Controller @RequestMapping("/router") @SessionAttributes(value = { "username" } ...

  9. spring boot: Whitelabel Error Page的解决方案

    http://blog.csdn.net/u014788227/article/details/53670112

  10. MySQL中concat以及group_concat的使用

    摘自:https://www.jianshu.com/p/43cb4c5d33c1 说明: 本文中使用的例子均在下面的数据库表tt2下执行: 一.concat()函数 1.功能:将多个字符串连接成一个 ...