查找最小的k个元素(数组)
2012-10-12 22:08
302 查看
// 5.查找最小的k个元素(数组)
// 题目:输入n个整数,输出其中最小的k个。
// 例如输入1,2,3,4,5,6,7和8这8个数字,则最小的4个数字为1,2,3和4。
// 注:
// 1 在数据量不大的情况下,排序
// 2 维护一个最小k 的数组 ,复杂度 为 o(k * N)
// 3 为一个最小K个数的最大堆 o(log2 k * N)
//每次看这些小算法都不以为然,每次都巨耗时间,比我想象的要多的多啊.....
#include <iostream>
#define N 30
using namespace std;
int gheap
= {0};
int count = 0;
void insertheap(int heap[],int data);
void adjustheap(int heap[]);
void popheap(int heap[], int &);
int main()
{
int data[] = {3,54,65,3,65,7,54,3,23,2,56,54,34};
int k;
cin>>k;
if (k<0)
{
cout<<"k should be bigger than 0"<<endl;
return 0;
}
for (int i=0; i<sizeof(data)/sizeof(int); ++i)
{
if (count<k)
{
insertheap(gheap,data[i]);
}
else
{
if (data[i]< gheap[0])
{
int d;
popheap(gheap,d); //将最大剔除,将比其小的加入
insertheap(gheap,data[i]);
}
}
}
// while (count)
// {
// int d;
// popheap(gheap,d);
// cout<<d<<" ";
// }
for (int j=0; j<k; j++)
{
int d;
popheap(gheap,d);
cout<<d<<" ";
}
cout<<endl;
return 0;
}
void swapdata(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
void insertheap(int heap[],int data)
{
int curpos,parent;
if (count>=N)
{
cout<<"error:heap is full"<<endl;
return;
}
heap[count] = data;
curpos = count++;
while(curpos)
{
parent = curpos / 2;
if (heap[curpos] > heap[parent])
{
swapdata(heap[curpos], heap[parent]);
curpos = parent;
}
else
{
break;
}
}
}
void adjustheap(int heap[])
{
int bigchild;
if (count<2)
{
return;
}
for (int i=0; 2*i<count; i++)
{
bigchild = 2*i+1;
if (bigchild+1< count && heap[bigchild+1]>heap[bigchild]) //choose the bigger child
{
bigchild++;
}
if (heap[i]<heap[bigchild])
{
swapdata(heap[i], heap[bigchild]);
}
}
}
void popheap(int heap[], int &data)
{
if (count==0)
{
cout<<"heap empty"<<endl;
return;
}
data = heap[0];
heap[0] = heap[--count];
adjustheap(heap);
}
// 题目:输入n个整数,输出其中最小的k个。
// 例如输入1,2,3,4,5,6,7和8这8个数字,则最小的4个数字为1,2,3和4。
// 注:
// 1 在数据量不大的情况下,排序
// 2 维护一个最小k 的数组 ,复杂度 为 o(k * N)
// 3 为一个最小K个数的最大堆 o(log2 k * N)
//每次看这些小算法都不以为然,每次都巨耗时间,比我想象的要多的多啊.....
#include <iostream>
#define N 30
using namespace std;
int gheap
= {0};
int count = 0;
void insertheap(int heap[],int data);
void adjustheap(int heap[]);
void popheap(int heap[], int &);
int main()
{
int data[] = {3,54,65,3,65,7,54,3,23,2,56,54,34};
int k;
cin>>k;
if (k<0)
{
cout<<"k should be bigger than 0"<<endl;
return 0;
}
for (int i=0; i<sizeof(data)/sizeof(int); ++i)
{
if (count<k)
{
insertheap(gheap,data[i]);
}
else
{
if (data[i]< gheap[0])
{
int d;
popheap(gheap,d); //将最大剔除,将比其小的加入
insertheap(gheap,data[i]);
}
}
}
// while (count)
// {
// int d;
// popheap(gheap,d);
// cout<<d<<" ";
// }
for (int j=0; j<k; j++)
{
int d;
popheap(gheap,d);
cout<<d<<" ";
}
cout<<endl;
return 0;
}
void swapdata(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
void insertheap(int heap[],int data)
{
int curpos,parent;
if (count>=N)
{
cout<<"error:heap is full"<<endl;
return;
}
heap[count] = data;
curpos = count++;
while(curpos)
{
parent = curpos / 2;
if (heap[curpos] > heap[parent])
{
swapdata(heap[curpos], heap[parent]);
curpos = parent;
}
else
{
break;
}
}
}
void adjustheap(int heap[])
{
int bigchild;
if (count<2)
{
return;
}
for (int i=0; 2*i<count; i++)
{
bigchild = 2*i+1;
if (bigchild+1< count && heap[bigchild+1]>heap[bigchild]) //choose the bigger child
{
bigchild++;
}
if (heap[i]<heap[bigchild])
{
swapdata(heap[i], heap[bigchild]);
}
}
}
void popheap(int heap[], int &data)
{
if (count==0)
{
cout<<"heap empty"<<endl;
return;
}
data = heap[0];
heap[0] = heap[--count];
adjustheap(heap);
}
相关文章推荐
- 字符串算法——查找有序数组旋转后的最小值(无重复元素)(Find Minimum in Rotated Sorted Array)
- 查找旋转数组中的最小值元素(剑指offer8)
- 5.查找最小的k个元素(数组)
- C++ 算法 查找旋转数组中的最小值 允许重复元素
- 5. 微软面试题: 查找最小的k个元素(数组)
- 字符串算法——查找有序数组旋转后最小值(有重复元素)(Find Minimum in Rotated Sorted Array II)
- 设计一个最优算法来查找一n个元素数组中的最大值和最小值。已知一种需要比较2n次的方法,请给一个更优的算法。
- 面试题15 - 查找最小的 k 个元素 [数组] / (堆优化 STL O(nlogk) )[STL]
- 程序员面试题目总结--数组(三)【旋转数组的最小数字、旋转数组中查找指定数、两个排序数组所有元素中间值、数组中重复次数最多的数、数组中出现次数超过一半的数】
- 面试题15 - 查找最小的 k 个元素 [数组] / (堆优化 STL O(nlogk) )[STL]
- 微软面试100道之 5 查找最小的k个元素(数组)
- 查找最小的k个元素(数组)
- 设计一个最优算法来查找n个元素数组中的最大值和最小值
- 给定数组,查找最小的k个元素或最大的k个元素
- 5.查找最小的k个元素(数组)
- 关于查找数组中最小的k个元素的解答、updated
- 给定数组,查找最小的k个元素或最大的k个元素 快速排序算法灵活应用
- 查找数组中第二个最小元素
- 8+查找一个旋转数组的最小元素
- 关于查找数组中最小的k个元素的解答、updated