归并排序
来源:互联网 发布:ant执行sql脚本 编辑:程序博客网 时间:2024/05/17 09:46
递归实现,挺麻烦,时间复杂度为O(nlogn),归并排序的过程,像极了一颗倒置的完全二叉树,高度为logn,可以理解为,对每一个元素都要进行深度为logn的归并。
空间复杂度为O(n+logn)
void Merge(int in[],int out[],int i,int m,int n)
{
int j,k,t;
for(k=i,j=m+1; i<=m && j<= n; k++)
{
if(in[i]<in[j])
{
out[k]=in[i];
i++;
}
else
{
out[k]=in[j];
j++;
}
}
if (i<=m)
{
for (t=0; t<=m; t++)
out[k+t]=in[i+t];
}
if (j<=n)
{
for (t=0; t<=m; t++)
out[k+t]=in[j+t];
}
}
void MSort(int arr[],int out[],int s,int t)
{
int m;
int TR2[MAXSIZE]={0};
if(s==t)
{
out[s]=arr[s];
}
else
{
m=(s+t)/2;
MSort(arr,TR2,s,m);
MSort(arr,TR2,m+1,t);
Merge(TR2,out,s,m,t);
}
}
void main()
{
int a[]={1, 5 , 2 };
const int length = sizeof(a)/sizeof(int);
int out[length]={0};
MSort(a,out,0,length-1);
int i=1;
for (i=0; i<length; i++)
printf("%d ",out[i]);
system("pause");
}
本文来自于《大话数据结构》
- 归并排序-归并排序
- 归并和归并排序
- 归并与归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 排序::归并
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- Sybase PowerDesigner v12.5 特别破解版
- 我的程序又core dump了
- “INSERT ON DUPLICATE KEY UPDATE”更新部分字段
- PHP框架文档
- Axure原型设计软件快捷键大全
- 归并排序
- vsftpd配置说明
- ubuntu 启动eclipse问题
- 在linux中添加ftp用户,并设置相应的权限
- linux SysV IPC msg消息队列实现
- oracle事件跟踪
- Web Services的起源和基本原理
- 【从测试数据看出你的密码是否安全】
- jquery事件对象属性