归并排序程序
来源:互联网 发布:思科交换机做端口镜像 编辑:程序博客网 时间:2024/05/18 01:47
void merge(int *a,int p,int q,int r)//将两段子数组排进一个临时数组
{
int beg1,end1,beg2,end2;
beg1 = p; end1 = q;
beg2 = q+1; end2 = r;
int *t = (int*)malloc((r-p+1)*sizeof(int));
int k = 0;
while((beg1 <= end1) && (beg2 <= end2))//比较小的放进去
{
if(a[beg1] < a[beg2])
{
t[k] = a[beg1++];
k++;
}
else
{
t[k] = a[beg2++];
k++;
}
}
while(beg1 <= end1)//剩余的直接放在后面
{
t[k] = a[beg1];
k++;
beg1++;
}
while(beg2 <= end2)
{
t[k] = a[beg2];
k++;
beg2++;
}
int m = 0;
for(int i = p;i <= r;i++)
{
a[i] = t[m];
m++;
}
free(t);
}
void guibing(int *a,int first,int last)
{
int mid = 0;
if(first < last)//必须是小于不能等于,否则死循环
{
mid = (first + last)/2;
guibing(a,first,mid);
guibing(a,mid+1,last);
merge(a,first,mid,last);
}
}
{
int beg1,end1,beg2,end2;
beg1 = p; end1 = q;
beg2 = q+1; end2 = r;
int *t = (int*)malloc((r-p+1)*sizeof(int));
int k = 0;
while((beg1 <= end1) && (beg2 <= end2))//比较小的放进去
{
if(a[beg1] < a[beg2])
{
t[k] = a[beg1++];
k++;
}
else
{
t[k] = a[beg2++];
k++;
}
}
while(beg1 <= end1)//剩余的直接放在后面
{
t[k] = a[beg1];
k++;
beg1++;
}
while(beg2 <= end2)
{
t[k] = a[beg2];
k++;
beg2++;
}
int m = 0;
for(int i = p;i <= r;i++)
{
a[i] = t[m];
m++;
}
free(t);
}
void guibing(int *a,int first,int last)
{
int mid = 0;
if(first < last)//必须是小于不能等于,否则死循环
{
mid = (first + last)/2;
guibing(a,first,mid);
guibing(a,mid+1,last);
merge(a,first,mid,last);
}
}
0 0
- 程序碎片-归并排序
- 归并排序程序
- 归并排序 非递归程序
- 归并排序之CC++程序
- 归并排序和快速排序C++程序
- JAVA程序完成归并排序算法
- C程序之4:向量归并排序
- H面试程序(18): 归并排序
- 归并排序-归并排序
- 9、C程序 数组排序之[归并排序]
- 归并和归并排序
- 归并与归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 数据结构---位运算
- HDU 5317(2015多校3)-RGCDQ(数论)
- org.apache.jasper.el.ELContextImpl cannot be cast to org.apache.jasper.el.ELContextImpl
- js之 prototype 理解补充
- 基于json-lib.jar包 创建JSONObject的四个常用方法
- 归并排序程序
- UVA10194getline()的用法注意
- ThinkPHP3.1.3使用phpExcel1.8.0实现数据从Excel表格导入mysql数据库
- ubuntu 14.04 安装chrome及给chromium 安装 flash player
- Perf -- Linux下的系统性能调优工具,第1部分
- HDU 5318 (dp+矩阵快速幂优化)
- Leetcode #241 Different Ways to Add Parentheses
- 限制某个进程只能在某个CPU上运行
- poj2395(最小生成树最大边)