获取数组中K个最大元素(k largest(or smallest) elements in an array | added Min Heap method)
来源:互联网 发布:号码复式软件 编辑:程序博客网 时间:2024/05/29 23:46
原文地址:http://www.geeksforgeeks.org/k-largestor-smallest-elements-in-an-array/
Question: Write an efficient program for printing k largest elements in an array. Elements in array can be in any order.
问题:写一个有效的程序打印数组中k个最大的元素。数组中的元素是任意顺序的。
For example, if given array is [1, 23, 12, 9, 30, 2, 50] and you are asked for the largest 3 elements i.e., k = 3 then your program should print 50, 30 and 23.
例如,如果一个数组是[1, 23, 12, 9, 30, 2, 50],那么你被要求输出的是前三个最大的元素,例如,k=3,那么你的程序应该打印50,30与23。
Method 1 (Use Bubble k times)
Thanks to Shailendra for suggesting this approach.
1) Modify Bubble Sort to run the outer loop at most k times.
2) Print the last k elements of the array obtained in step 1.
Time Complexity: O(nk)
Like Bubble sort, other sorting algorithms like Selection Sort can also be modified to get the k largest elements.
方法1(用k次冒泡排序)
1)修改冒泡排序,让外部循环最多跑k次。
2)打印第一步得到的数组中最后的k个元素。
时间复杂度是O(nk)
类似于冒泡排序,其他排序算法想选择排序也可以修改成活的最大的k个元素
Method 2 (Use temporary array)
K largest elements from arr[0..n-1]
1) Store the first k elements in a temporary array temp[0..k-1].
2) Find the smallest element in temp[], let the smallest element be min.
3) For each element x in arr[k] to arr[n-1]
If x is greater than the min then remove min from temp[] and insertx.
4) Print final k elements of temp[]
Time Complexity: O((n-k)*k). If we want the output sorted then O((n-k)*k + klogk)
Thanks to nesamani1822 for suggesting this method.
方法2(用临时数组)
数组arr[0..n-1]中K个最大元素
1)将前k个元素保存在临时数组temp[[0..k-1]中
2)找到在temp[]中最小的元素,让min等于最小的元素
3)对于arr[k]到arr[n-1]之间的每个元素x,如果x大于min,那么移除temp[]中的min,然后插入x
4)打印temp[]中最后k个元素
Method 3(Use Sorting)
1) Sort the elements in descending order in O(nLogn)
2) Print the first k numbers of the sorted array O(k).
Time complexity: O(nlogn)
方法3(用排序)
1)在时间复杂度为O(nLogn)的范围内降序排序
2)用O(k)的时间打印已排好序的数组的前k个元素
Method 4 (Use Max Heap)
1) Build a Max Heap tree in O(n)
2) Use Extract Max k times to get k maximum elements from the Max Heap O(klogn)
Time complexity: O(n + klogn)
方法4(用最大堆)
1)在O(n)时间内建立一个最大堆
2)在时间O(klogn)内用抽取最大k次从最大堆获得最大元素
时间复杂度是:O(n + klogn)
Method 5(Use Oder Statistics)
1) Use order statistic algorithm to find the kth largest element. Please see the topic selection in worst-case linear time O(n)
2) Use QuickSortPartition algorithm to partition around the kth largest number O(n).
3) Sort the k-1 elements (elements greater than the kth largest element) O(kLogk). This step is needed only if sorted output is required.
Time complexity: O(n) if we don’t need the sorted output, otherwise O(n+kLogk)
Thanks to Shilpi for suggesting the first two approaches.
方法5(用订单统计法)
1)用订单统计算法来寻找第k个最大的元素。
2)在O(n)时间内用快速排序分区算法来分区第k个最大的元素。
3)在O(kLogk)时间对k-1个元素进行排序(这些元素都大于第k个最大的元素)。只有在需要输出结果有序的时候才需要这一步。
时间复杂度:如果我们不需要有序的输出则时间复杂度为O(n),否则是O(n+kLogk)
Method 6 (Use Min Heap)
This method is mainly an optimization of method 1. Instead of using temp[] array, use Min Heap.
Thanks to geek4u for suggesting this method.
1) Build a Min Heap MH of the first k elements (arr[0] to arr[k-1]) of the given array. O(k)
2) For each element, after the kth element (arr[k] to arr[n-1]), compare it with root of MH.
……a) If the element is greater than the root then make it root and call heapify for MH
……b) Else ignore it.
// The step 2 is O((n-k)*logk)
3) Finally, MH has k largest elements and root of the MH is the kth largest element.
Time Complexity: O(k + (n-k)Logk) without sorted output. If sorted output is needed then O(k + (n-k)Logk + kLogk)
方法6:(用小顶堆)
这个方法主要是方法1的优化,用小顶堆而不是临时数组temp[]。
1)建立一个由给定数组前k个元素(arr[0]到arr[k-1])在时间O(k)内的小顶堆MH。
2)对于每一个元素,在k个元素与MH的根元素做了比较以后。
……a)如果元素大于根节点,那么让这个元素作为根节点,调用堆调整算法。
……b)否则略过。
//第二步的时间复杂度是O((n-k)*logk)
3)最后,MH已经有了最大的k个元素,并且MH的跟是第k个最大的元素。
时间复杂度是:无需有序的输出:O(k + (n-k)Logk),如果输出需要排序,那么时间复杂度为:O(k + (n-k)Logk + kLogk)
All of the above methods can also be used to find the kth largest (or smallest) element.
以上方法都可以用于寻找第k个最大(或者最小)的元素。
Please write comments if you find any of the above explanations/algorithms incorrect, or find better ways to solve the same problem.
References:
http://en.wikipedia.org/wiki/Selection_algorithm
- 获取数组中K个最大元素(k largest(or smallest) elements in an array | added Min Heap method)
- Kth Largest Element in an Array(数组中第K大元素)
- 215. Kth Largest Element in an Array(数组中第k大元素)
- Leetcode 215 Kth Largest Element in an Array 数组中第k大的元素
- LeetCode OJ 之 Kth Largest Element in an Array(数组中的第k大元素)
- 215. Kth Largest Element in an Array(数组中第k大的数字)
- [LeetCode] Kth Largest Element in an Array (找出数组的第k大的元素)
- 字符串算法——查找数组第K个最大值( Kth Largest Element in an Array)
- [LinkedIn] Smallest/greatest/largest k element in stream of data (integer array) priority Q / heap
- LeetCode 215. Kth Largest Element in an Array(第K大元素)
- 【Top K 问题】[Leetcode-215] Kth Largest Element in an Array 数组中第K大的数
- 数组-Kth Largest Element in an Array(找出第K大的数)
- The kth largest element in max-heap 最大堆的第k大元素
- leetcode215---Kth Largest Element in an Array(第k大元素)
- 【LeetCode】215. Kth Largest Element in an Array找第K大的元素
- 【LeetCode-面试算法经典-Java实现】【215-Kth Largest Element in an Array(数组中第K大的数)】
- Kth Largest Element in an Array 数组中的第K大的数
- 347. Top K Frequent Elements(找出数组中出现次数最多的前k个元素)
- React Native 自定义圆角button的封装
- Verilog中的批处理generate和缩减运算符
- 输入不定长正整数,任意组合,输出最大值。
- 解决前台jsp中中文提交到后台乱码问题
- js中apply或call的时间开销
- 获取数组中K个最大元素(k largest(or smallest) elements in an array | added Min Heap method)
- 倒三角形 java实现
- Java解析XML文档方法笔记
- 387. First Unique Character in a String
- C#编程基础(五)
- BZOJ 3876: [Ahoi2014]支线剧情【下界费用流
- js-更换网页皮肤
- VS2010 下配置使用 Python3.5
- JAVA从菜鸟【入门】到新手【实习】一一一一算法,数据结构与数据库