归并排序的问题

djjlove_2008 2010-04-09 10:43:52
#include <iostream>
using namespace std;

void Merge(int a[], int tmp[], int lPos, int rPos, int rEnd)
{
int lEnd = rPos - 1;
int tmpPos = lPos;

while(lPos <= lEnd && rPos <= rEnd)
{
if(a[lPos] <= a[rPos])
tmp[tmpPos++] = a[lPos++];
else
tmp[tmpPos++] = a[rPos++];
}

while(lPos <= lEnd)
tmp[tmpPos++] = a[lPos++];

while(rPos <= rEnd)
tmp[tmpPos++] = a[rPos++];

int numElements = rEnd - lPos + 1;
for(int i; i < numElements; i++)
a[i] = tmp[i];

}

void Msort(int a[], int tmp[], int low, int high)
{
if(low < high)
return;
int middle = (low + high) / 2;

Msort(a, tmp, low, middle);
Msort(a, tmp, (middle + 1), high);
Merge(a, tmp, low, (middle + 1), high);
}

void MergeSort(int a[], int len)
{
int *tmp = new int[len];
if(tmp != NULL)
{
Msort(a, tmp, 0, len - 1);
delete []tmp;
}
}

int main()
{
int a[] = {1, 3, 4, 2, 8, 5, 6};
MergeSort(a, 7);

cout << "The sorted array is:" << endl;
for(int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
cout << a[i] << " ";
cout << endl;

system("pause");
return 0;
}

这道归并排序为为什么不想作用了?
...全文
160 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
djjlove_2008 2010-04-13
  • 打赏
  • 举报
回复
谢谢各位
djjlove_2008 2010-04-09
  • 打赏
  • 举报
回复
//通用性很好的一道归并排序,妙哉
#include <iostream>
using namespace std;

void Merge(int a[], int tmp[], int lPos, int rPos, int rEnd)
{
int i, lEnd, NumElements, tmpPos;
lEnd = rPos - 1;
tmpPos = lPos;
NumElements = rEnd - lPos + 1;

while(lPos <= lEnd && rPos <= rEnd)
{
if(a[lPos] <= a[rPos])
tmp[tmpPos++] = a[lPos++];
else
tmp[tmpPos++] = a[rPos++];
}

while(lPos <= lEnd)
tmp[tmpPos++] = a[lPos++];
while(rPos <= rEnd)
tmp[tmpPos++] = a[rPos++];

for(i = 0; i < NumElements; i++, rEnd--)
a[rEnd] = tmp[rEnd];
}

void msort(int a[], int tmp[], int low, int high)
{
if(low >= high)
return;
int middle = (low + high) / 2;
msort(a, tmp, low, middle);
msort(a, tmp, (middle + 1), high);
Merge(a, tmp, low, (middle + 1), high);
}

void merge_sort(int a[], int len)
{
int *tmp = NULL;
tmp = new int[len];
if(tmp != NULL)
{
msort(a, tmp, 0, len - 1);
delete []tmp;
}
}

void print_array(int a[], int len)
{
for(int i = 0; i < len; i++)
cout<<a[i]<<" ";
cout<<endl;
}

int main()
{
int a[8] = {8, 6, 1, 3, 5, 2, 7, 4};
cout<<"The original array:"<<endl;
print_array(a, 8);
merge_sort(a, 8);
cout<<"The sorted array:"<<endl;
print_array(a, 8);

system("pause");
return 0;
}


这样改过之后仍然存在,大家看看有什么区别了,呵呵,,,共同进步...
djjlove_2008 2010-04-09
  • 打赏
  • 举报
回复
不是这样的,这样实现只是为了她的通用性更好,这道程序我写了半天,按照书上的提示的思想,写的.
#include <iostream>
using namespace std;

void Merge(int a[], int tmp[], int lPos, int rPos, int rEnd)
{
int lEnd = rPos - 1;
int tmpPos = lPos;

while(lPos <= lEnd && rPos <= rEnd)
{
if(a[lPos] <= a[rPos])
tmp[tmpPos++] = a[lPos++];
else
tmp[tmpPos++] = a[rPos++];
}

while(lPos <= lEnd)
tmp[tmpPos++] = a[lPos++];

while(rPos <= rEnd)
tmp[tmpPos++] = a[rPos++];

int numElements = rEnd - lPos + 1;
for(int i = 0; i < numElements; i++)
a[i] = tmp[i];

}

void Msort(int a[], int tmp[], int low, int high)
{
if(low < high)
return;
int middle = (low + high) / 2;

Msort(a, tmp, low, middle);
Msort(a, tmp, (middle + 1), high);
Merge(a, tmp, low, (middle + 1), high);
}

void MergeSort(int a[], int len)
{
int *tmp = new int[len];
if(tmp != NULL)
{
Msort(a, tmp, 0, len - 1);
delete []tmp;
}
}

int main()
{
int a[] = {1, 3, 4, 2, 8, 5, 6};
MergeSort(a, 7);

cout << "The sorted array is:" << endl;
for(int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
cout << a[i] << " ";
cout << endl;

system("pause");
return 0;
}
这样改过之后仍然有问题存在.排序的问题仍然不存在,你们运行试试..
走好每一步 2010-04-09
  • 打赏
  • 举报
回复
楼上正解。
楼主对while循环用的不错,不过尽量使用就近原则。
void Merge(int a[], int tmp[], int lPos, int rPos, int rEnd);
这个接口设计有问题,temp数组应该在此函数内开辟,毕竟参数少点更清晰。
输入:数组首地址,排序好的两部分下标。
输出:排序数组。

而不是
输入:数组首地址,采用一个temp数组放??,同上
输出:同上。

暴露自己的实现只会让使用者迷惑。
macrojj 2010-04-09
  • 打赏
  • 举报
回复
for(int i; i < numElements; i++)
a[i] = tmp[i];


int i=0

谢谢
AlanBruce 2010-04-09
  • 打赏
  • 举报
回复
UP

UP

64,671

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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