归并排序,错在哪?

xpjandy 2010-09-16 07:52:36

#include "stdafx.h"
#include "stdio.h"
#include "iostream"
using namespace std;
//src[l..m] and src[m+1 ... h]
void merge(int src[],int dst[],int l,int m,int h)
{
int i=l,j=m+1,k=l;
while(i<=m&&j<=h)
{
if (src[i]<src[j])
{
dst[k++]=src[i++];
}
else
{
dst[k++]=src[j++];
}
}
while(i<=m)
{
dst[k++]=src[i++];
}
while(j<=h)
{
dst[k++]=src[j++];
}
}
// put the sorted num into dst[]
void merge_sort(int src[],int dst[],int low,int high)
{
if (low<high)
{
int mid=low+(high-low)/2;
merge_sort(src,dst,low,mid);
merge_sort(src,dst,mid+1,high);
merge(src,dst,low,mid,high);
}
}
void main()
{
int a[4]={4,3,6,1};
int b[4];
merge_sort(a,b,0,3);
}



将数组a排序,结果到数组b中,排序结果不对,看不出问题在哪。。。
...全文
80 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
honghu069 2010-09-16
  • 打赏
  • 举报
回复
merge 里还要执行一个操作,要把 dst数组 赋值给 src数组
xpjandy 2010-09-16
  • 打赏
  • 举报
回复

有什么区别么
数组名做参数相当于指针了吧?
gnefuil 2010-09-16
  • 打赏
  • 举报
回复
把src和dst换成(*src)和(*dst)?

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧