寻找最小的K个数
来源:互联网 发布:用淘宝充话费怎么退款 编辑:程序博客网 时间:2024/06/09 23:40
解法一:排序法:
//解法一:全部排序//时间复杂度:O(n*n);#include <iostream>using namespace std;template<class T>void Sort(T array[], size_t size, int k){ for (int i = 0; i < size; i++) { for (int j = i + 1; j < size; j++) { if (array[i] > array[j]) { swap(array[i], array[j]); } } } for(int idx=0; idx<k; idx++) cout<<array[idx]; cout<<endl;}void FunTest(){ int arr[] = {1, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 2, 3, 4, 5, 6, 7, 8, 9, 10, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90}; Sort(arr, sizeof(arr)/sizeof(arr[0]), 5);}int main(){ FunTest(); system("pause"); return 0;}
解法二:创建大堆(k个),取堆顶,调整
(1)用容量为k的最大堆存储最先遍历的k个数,假设他们是最小的k个数,建堆用时O(k),建好堆后堆中的元素是有序的,可令K1
//解法二:创建大堆(k个),取堆顶,调整#include <vector>#include <iostream>using namespace std;#include <assert.h>template<class T>struct Less{ bool operator()(const T& left, const T& right)//仿函数 { return left<right; }};template<class T>struct Greater{ bool operator()(const T& left, const T& right)//仿函数 { return left>right; }};//模板参数template<class T, class Compare = Less<T>>class Heap{public: // 创建一个空堆 Heap() {} Heap(const T array[], size_t size) { _heap.resize(size); //一次性开辟足够的空间 for(size_t idx=0; idx<size; ++idx) { //_heap.push_back(array[idx]); //先开辟有限个,若不够,再开辟 _heap[idx] = array[idx]; //如果没有扩容,就是没有开辟空间,导致程序崩溃 } int root = (_heap.size() - 2)>>1; //找到最后一个非叶子节点 for(; root>=0; root--) { _AdjustDown(root); } } size_t Size()const { return _heap.size(); } bool Empty()const { return _heap.empty(); } void Insert(const T& data) { _heap.push_back(data); if(_heap.size()>1) _AdjustUp(); } T& Top() { assert(!Empty()); return _heap[0]; } void Remove() { assert(!_heap.empty()); size_t last = _heap.size()-1; swap(_heap[last], _heap[0]); _heap.pop_back(); _AdjustDown(0); }protected: void _AdjustDown(size_t parent) { size_t size = _heap.size(); size_t child = 2*parent+1; //左孩子结点 while(child<size) { child = 2*parent+1; //右孩子存在,找出左右孩子中小的节点 Compare com; if(child+1 < size && com(_heap[child+1], _heap[child])) { child+= 1; //child指向右孩子 } if(com(_heap[child], _heap[parent])) { swap(_heap[child], _heap[parent]); parent = child; child = parent*2+1; } else { break; } } } void _AdjustUp() { size_t child = _heap.size()-1; size_t parent = (child-1)>>1; while(child != 0) { Compare com; if(com(_heap[child], _heap[parent])) { swap(_heap[child], _heap[parent]); child = parent; parent = (child-1)>>1; } else return; } }protected: std::vector<T> _heap;};void FunTest(){ int array[] = {100, 99, 98, 97, 96, 95, 94, 93, 92, 91}; Heap<int, Greater<int>> hp(array, sizeof(array)/sizeof(array[0])); int arr[] = {1, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 2, 3, 4, 5, 6, 7, 8, 9, 10, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90}; int len = sizeof(arr)/sizeof(arr[0]); for(int i=0; i<len; ++i) { int ret = arr[i]; if(hp.Top() > ret) { hp.Remove(); hp.Insert(ret); } }}int main(){ FunTest(); system("pause"); return 0;}
0 0
- 寻找最小的k个数
- 寻找最小的k个数
- 寻找最小的K个数
- 寻找最小的k个数
- 寻找最小的K个数
- 寻找最小的k个数
- 寻找最小的k个数
- 寻找最小的k个数
- 寻找最小的k个数
- 寻找最小的k个数
- 寻找最小的k个数
- 寻找最小的k个数
- 寻找最小的 k 个数
- 寻找最小的k个数
- 寻找最小的k个数
- 寻找最小的k个数
- 寻找最小的k个数
- 寻找最小的K个数
- ACM:R: Optimal Parking
- 商品展示
- STM32的GPIO有几种输出模式及其说明
- 欢迎使用CSDN-markdown编辑器
- 洛谷 P2024 食物链 【并查集+完美解释】
- 寻找最小的K个数
- webService实用性文章
- 山东省第八届ACM省赛 Problem.H triangle
- abstract抽象类和抽象方法
- CTSC 2017
- 第五次上机实验--数组分离
- springmvc之定时器
- 机器学习----分类问题
- 洛谷3765总统选举暨洛谷5月月赛R1B题