Maintain a min-heap with size = k, to collect the result.

 //Find K minimum values from an unsorted array
//Implement Min-Heap public int[] findKMax(int[] arr, int k){
if(arr == null || arr.length == 0) return null;
int[] result = new int[k];
for(int i = 0; i < k; i ++)
result[i] = arr[i];
buildMinHeap(result);
for(int i = k; i < arr.length; i ++){
if(arr[i] > result[0]){
result[0] = arr[i];
minHeapify(result, 0);
}
}
return result;
} public void buildMinHeap(int[] arr){
for(int i = arr.length / 2 - 1; i > -1; i --){//bottom-up build min heap
minHeapify(arr, i);
}
} public void minHeapify(int[] arr, int curIndex){
int left = curIndex * 2 + 1;
int right = curIndex * 2 + 2;
int smallest = curIndex;
if(left < arr.length && arr[left] < arr[smallest])
smallest = left;
if(right < arr.length && arr[right] < arr[smallest])
smallest = right;
if(smallest != curIndex){
swap(arr, smallest, curIndex);
minHeapify(arr,smallest);
}
} public void swap(int[] arr, int aa, int bb){
int tmp = arr[aa];
arr[aa] = arr[bb];
arr[bb] = tmp;
}

find K maximum value from an unsorted array(implement min heap)的更多相关文章

  1. Why is processing a sorted array faster than an unsorted array?

    这是我在逛 Stack Overflow 时遇见的一个高分问题:Why is processing a sorted array faster than an unsorted array?,我觉得这 ...

  2. Kth Smallest Element in Unsorted Array

    (referrence: GeeksforGeeks, Kth Largest Element in Array) This is a common algorithm problem appeari ...

  3. [Swift]LeetCode325. 最大子数组之和为k $ Maximum Size Subarray Sum Equals k

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  4. Why is processing a sorted array faster than an unsorted array(Stackoverflow)

    What is Branch Prediction? Consider a railroad junction: Image by Mecanismo, via Wikimedia Commons. ...

  5. 77 找出最大连续自然数个数[Longest Consecutive Sequence in an Unsorted Array]

    [本文链接] http://www.cnblogs.com/hellogiser/p/Longest-Consecutive-Sequence-in-an-Unsorted-Array.html [题 ...

  6. Data Structure Array: Find the two numbers with odd occurrences in an unsorted array

    http://www.geeksforgeeks.org/find-the-two-numbers-with-odd-occurences-in-an-unsorted-array/ #include ...

  7. 【leetcode】Maximum Gap

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  8. 【LeetCode】164. Maximum Gap (2 solutions)

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  9. 【刷题-LeetCode】164 Maximum Gap

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

随机推荐

  1. 【mysql】排序方法

    查询各科成绩前三名的记录,不考虑并列的情况: select a.course_id as 课程ID, a.score as 成绩, count(a.course_id) as 排名 from scor ...

  2. FFM算法解析及Python实现

    1. 什么是FFM? 通过引入field的概念,FFM把相同性质的特征归于同一个field,相当于把FM中已经细分的feature再次进行拆分从而进行特征组合的二分类模型. 2. 为什么需要FFM? ...

  3. Keepalived高可用集群

    一.服务介绍 keepalive起初是专为LVS设计的,专门用来监控LVS集群系统红各个服务节点的状态,后来又加入了VRRP的功能,因此不了配合LVS服务外,也可以作为其他服务(nginx,hapro ...

  4. 使用Fiddler模拟客户端http响应【转】

    转自:使用Fiddler模拟客户端http响应 在客户端开发中,常常需要对一些特殊情况做处理,比如404.503等,又比如服务返回错误数据等.而测试这些情况会比较麻烦,往往都是找开发人员配合修改代码, ...

  5. QTP日常积累

    1.init同步测试对象 同步测试对象: CODE: Browser("百度一下,你就知道").Page("百度一下,你就知道").WebEdit(" ...

  6. jmeter☞工作区介绍(三)

    基于jmeter4.0,jdk1.8 目录树:存放设计过程中使用的元件.执行过程中默认是从根节点开始顺序遍历元件.比如说HTTP请求的取样器就是元件,组件就是一个或多个元件的集合. 测试计划编辑区域: ...

  7. python魔法方法大全

    1.python魔法方法详解: python魔法方法是可以修改重载的,如果你的对象实现(重载)了这些方法中的某一个,那么这个方法就会在特殊的情况下被 Python 所调用,你可以定义自己想要的行为,而 ...

  8. Python里的类和对象简介

    ---恢复内容开始--- Python里的类  对象=属性+方法: 对象的属性主要是指主要的特征和参量,而方法主要是指函数: 类是一个具有一定特征和方法的集合,而对象是类的一个:类和对象的关系就如同模 ...

  9. 【坚持】Selenium+Python学习之从读懂代码开始 DAY7

    2018/05/25 EC [EC](https://github.com/easonhan007/webdriver_guide/blob/master/34/expected_conditions ...

  10. Webrtc源码走读(一)

    阅读event_wrapper.h   event_wrapper_win.cpp 的实现 自己对“事件”这个词没有深的理解,通过看段代码,好像有点感觉,类似与C#的AutoResetEvent