To_10_r_100_5---查找最小的K个元素
来源:互联网 发布:好易网络电视tv版下载 编辑:程序博客网 时间:2024/05/16 09:44
题目:
输入n个整数,输出其中最小的K个。
思路:
1. 复杂度O(nlogn) :快排之类,然后输出最小的K个。
2. 复杂度O(kn) :遍历找k次最大值
3. 堆排序O(n+klogn):建堆O(n),取出k个最小值O(klogn)。
C++代码:
3. 堆排序
// Heap class
template <class E> class heap{
private:
E* Heap; // Pointer to the heap array
int maxsize; // Maximum size of the heap
int n; // Number of elements now in the heap
bool Prior(E x, E y){
return x < y;
}
void swap(E* source, int x, int y){
E temp = source[x];
source[x] = source[y];
source[y] = temp;
}
void Assert(bool test, string error){
if (!test){
cout << error << endl;
exit(1);
}
}
// Helper function to put element in its correct place
void siftdown(int pos){
while (!isLeaf(pos)){ // Stop if pos is a leaf
int j = leftchild(pos);
int rc = rightchild(pos);
if ((rc < n) && Prior(Heap[rc], Heap[j]))
j = rc; // Set j to greater child's value
if (Prior(Heap[pos], Heap[j]))
return; //Done
swap(Heap, pos, j);
pos = j; //Move down
}
}
public:
heap(E* h, int num, int max) // Constructor
{
Heap = h;
n = num;
maxsize = max;
buildHeap();
}
int size() const{
return n;
}
bool isLeaf(int pos) const{
return (pos >= n / 2) && (pos < n);
}
int leftchild(int pos) const{
return pos * 2 + 1;
}
int rightchild(int pos) const{
return pos * 2 + 2;
}
int parent(int pos) const{
return (pos - 1) / 2;
}
void buildHeap(){ //从暂时的堆的最底层父节点开始往上层调整
for (int i = n / 2 - 1; i >= 0; i--)
siftdown(i);
}
// Insert "it" into the heap
void insert(const E& it){
Assert(n < maxsize, "Heap is full");
int cur = n++;
Heap[curr] = it; // Start at end of heap
// Now sift up until curr's parnt>curr
while ((curr != 0) && (Prior(Heap[curr], Heap[parent(curr)]))){ //只跟踪插入元素进行追踪调整
swap(Heap, curr, parent(curr));
curr = parent(curr);
}
}
// Remove first value
E removefirst(){
Assert(n > 0, "Heap is empty");
swap(Heap, 0, --n);
if (n != 0)
siftdown(0); // Siftdown new root val
return Heap[n]; // Return deleted value
}
// Remove and return element at specified position
E remove(int pos){
Assert((pos >= 0) && (pos < n), "Bad position");
if (pos == (n - 1))
n--; // Last element, no work to do
else{
swap(Heap, pos, --n); //Swap with last value
while ((pos != 0) && (Prior(Heap[pos], Heap[parent(pos)]))){ // Push up large key
swap(Heap, pos, parent(pos));
pos = parent(pos);
}
if (n != 0)
siftdown(pos); // Push down small key
}
return Heap[n];
}
void
};
int main(){
int source[7] = { 7, 4, 6, 1, 2, 3, 5 };
heap<int> test(source, 7, 100);
cout << test.removefirst() << endl;
cout << test.removefirst() << endl;
cout << test.removefirst() << endl;
cout << test.removefirst() << endl;
return 0;
}
- To_10_r_100_5---查找最小的K个元素
- 查找最小的k个元素
- 查找最小的k个元素
- 查找最小的 k个元素
- 查找最小的 K 个元素
- 查找最小的k个元素
- 5.查找最小的k个元素
- 查找最小的k个元素
- 查找最小的k个元素
- 查找最小的k个元素
- 5.查找最小的k个元素
- 查找最小的k个元素(数组)
- 查找最小的K个元素
- 查找最小的K个元素
- 查找最小的k个元素
- 查找最小的k个元素[算法]
- 查找最小的k 个元素
- 查找最小的k个元素
- sgu264:Travel(稳定婚姻问题)
- Effective Java 1:考虑使用静态工厂方法代替构造器
- 隐藏Ribbon的右键菜单
- 新的个人博客开通!!!
- POJ 1005
- To_10_r_100_5---查找最小的K个元素
- (自创)世界上最美丽浪漫的函数组合
- Cocos2d-X手游源码/iOS/Android/cocos2dx源码/AppStore/手游资源“集中营”
- Oracle数据库之PL/SQL程序设计基础
- “Lc.exe已退出 代码为-1”的解决办法
- X-code 运行原理
- Ubuntu 14.04 安装笔记
- 在Ubuntu 12.04中安装和配置vmware tools
- Android 5.0学习之使用Palette