你堆排序知多少?
来源:互联网 发布:java重载和重写 编辑:程序博客网 时间:2024/04/29 03:10
面试了很多的公司,根据同学的面试经验和自己的面试经验,现在整理出关于堆排序的实现代码。
// HeapSort.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>using namespace std;//以下是针对堆进行调整void HeapAjust(int data[],int i,int length){int nChild;int nTemp;for(nTemp=data[i];2*i+1<length;i=nChild){nChild=2*i+1;if(nChild<length-1&&data[nChild+1]>data[nChild])//比较哪个孩子比自己大,如果是右孩子的话,就要将nChild++;{nChild++;}if(nTemp<data[nChild])//如果比自己的最大的孩子小,就交换{data[i]=data[nChild];data[nChild]=nTemp;}else//如果比最大的孩子还大,就不交换break;}}//堆排序void HeapSort(int data[],int length){for(int i=(length>>1)-1;i>=0;i--)//注意这个地方:i=(length>>1)-1,加上括号,原因:优先级的问题{HeapAjust(data,i,length);//初始化一个堆}for(int j=length-1;j>0;--j){int temp=data[j];data[j]=data[0];data[0]=temp;HeapAjust(data,0,j);}}int _tmain(int argc, _TCHAR* argv[]){int a[]={4,5,8,6,1,7};int len=sizeof(a)/sizeof(int);HeapSort(a,len);for (int i=0;i<len;i++){cout<<a[i]<<" ";}cout<<endl;system("pause");return 0;}
// HeapSort.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
//以下是针对堆进行调整
void HeapAjust(int data[],int i,int length)
{
int nChild;
int nTemp;
for(nTemp=data[i];2*i+1<length;i=nChild)
{
nChild=2*i+1;
if(nChild<length-1&&data[nChild+1]>data[nChild])//比较哪个孩子比自己大,如果是右孩子的话,就要将nChild++;
{
nChild++;
}
if(nTemp<data[nChild])//如果比自己的最大的孩子小,就交换
{
data[i]=data[nChild];
data[nChild]=nTemp;
}
else//如果比最大的孩子还大,就不交换
break;
}
}
//堆排序
void HeapSort(int data[],int length)
{
for(int i=(length>>1)-1;i>=0;i--)//注意这个地方:i=(length>>1)-1,加上括号,原因:优先级的问题
{
HeapAjust(data,i,length);//初始化一个堆
}
for(int j=length-1;j>0;--j)
{
int temp=data[j];
data[j]=data[0];
data[0]=temp;
HeapAjust(data,0,j);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[]={4,5,8,6,1,7};
int len=sizeof(a)/sizeof(int);
HeapSort(a,len);
for (int i=0;i<len;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
system("pause");
return 0;
}
上面主要是功能函数,主函数大家搞定啦!我调试通过了已经!
堆排序的时间复杂度为O(nlogn),最坏的情况也是这个时间复杂度,空间复杂度是O(1)。但是堆排序是不稳定的!
在面试过程中,很多时候都会用到堆排序,比如下面的题目都是堆排序的典型题目:
1.给你100w个数据求最大的10个元素。这个时候我们可以使用小顶堆!这个是为什么呢!
2.给你100w个数据求最小的10个元素。这个时候我们可以使用大顶堆!这个是为什么呢!
相信会有很多同学会问出上面的两个疑问,答案其实很简单,在求最大的元素的时候,我们建立一个有10个元素的小顶堆,那么堆顶元素肯定是最小的,然后拿剩余的元素和堆顶进行比较,如果比堆顶大,就替换这个元素,然后调整堆,调整完之后堆顶依然是10个元素中最小的,依次比较剩余的元素。
堆排序与直接插入排序的区别
直接选择排序中,为了从R[1..n]中选出关键字最小的记录,必须进行n-1次比较,然后在R[2..n]中选出关键字最小的记录,又需要做n-2次比 较。事实上,后面的n-2次比较中,有许多比较可能在前面的n-1次比较中已经做过,但由于前一趟排序时未保留这些比较结果,所以后一趟排序时又重复执行 了这些比较操作。 堆排序可通过树形结构保存部分比较结果,可减少比较次数。
参考:
- 你堆排序知多少?
- static你知多少?
- const你知多少?
- assert你知多少?
- __attribute__ 你知多少?
- __attribute__ 你知多少?
- __attribute__ 你知多少?
- __attribute__ 你知多少?
- __attribute__ 你知多少?
- __attribute__ 你知多少?
- __attribute__ 你知多少?
- __attribute__ 你知多少?
- __attribute__ 你知多少?
- __attribute__ 你知多少?
- nstimer你知多少
- __attribute__ 你知多少?
- __attribute__ 你知多少?
- __attribute__ 你知多少?
- 图片和文字在DIV中垂直居中显示(兼容IE6,IE7和FF)
- 如何實現wxPython下的檔案拖放
- Spring 学习总结(一)依赖注入的理解
- 对CSDN博客文章的访问量的质疑
- Command.CreateParameter参数说明
- 你堆排序知多少?
- ZOJ 1649 Rescue (BFS) -- from lanshui_Yang
- poj 3080 kmp水题
- 关闭mysql的日志
- wxPython实现拖放实例
- 在WinXP下安装运行object-c的开发环境
- 使用objdump看内核源码(建议看kbuild文档)
- HttpClient发送请求
- C++类型转换总结