69,382
社区成员
发帖
与我相关
我的任务
分享
void Merge(int a[],int low,int middle,int high)
{
int h,i,j,k;
// int b[10];
int* b = NULL;
b = (int*)malloc((high+1)*sizeof(int));
h=low;
i=low;
j=middle+1;
while(h<=middle&&j<=high)
{
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
if(h>middle)
{
for(k=j;k<=high;k++)
{
b[i]=a[k];
i++;
}
}
else
{
for(k=h;k<=middle;k++)
{
b[i]=a[k];
i++;
}
}
for(k=low;k<=high;k++)
a[k]=b[k];
free(b);
}
//改了下代码,这么写肯定不会内存泄漏,有些问题既然不必要,又不好解决,就尽量避免啊^_^
#include "stdio.h"
#include "string.h"
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
void Merge(int a[],int low,int middle,int high)
{
int h,i,j,k;
int b[10];
h=low;
i=low;
j=middle+1;
while(h<=middle&&j<=high)
{
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
if(h>middle)
for(k=j;k<=high;k++)
{
b[i]=a[k];
i++;
}
else
{
for(k=h;k<=middle;k++)
{
b[i]=a[k];
i++;
}
}
for(k=low;k<=high;k++)
{
a[k]=b[k];
}
}
int MSort(int* In,int low,int high)
{//将In[s..t]自身归并排序
int middle;
if (low<high)
{
middle= (low+high)/2; //将In[s..t]平分为In[s..m]和In[m+1..t]
MSort(In,low,middle); //递归In[s..m]归并为有序的Out2[s..m]
MSort(In,middle+1,high); //递归In[m+1..t]归并为有序的Out2[m+1..t]
Merge(In,low,middle,high); //将Out2[s..m]和Out2[m+1..t]归并到Out1[s..t]
}
return 0;
}
int main(int argc, char* argv[])
{
_CrtMemState s1, s2, s3; // 声明内存状态结构变量
_CrtMemCheckpoint( &s1 );// 获取第一个内存状态快照
int in[]= {2,6,45,7,79,32,5,12,15,25};
MSort(in,0,9);
for(int i=0;i <10;i++)
printf("%d ",in[i]);
printf("\n");
_CrtMemCheckpoint( &s2 );// 获取第二个内存状态快照
// 比较两个内存快照的差异
if ( _CrtMemDifference( &s3, &s1, &s2) )
_CrtMemDumpStatistics( &s3 );// dump 差异结果
// 输出内存泄漏报告,显示在 Output 窗口的 Debug 页中
_CrtDumpMemoryLeaks();
return 0;
}
int* Out2 = (int*)malloc(t*sizeof(int));
if(s == t)
{
Out1[s] = In[s];
free(Out2);
}
class MemCtrl
{
public:
MemCtrl();
MemCtrl(int size);
virtual ~MemCtrl();
static int* p;
};
// implementation of the MemCtrl class.
int* MemCtrl::p = NULL;
MemCtrl::MemCtrl()
{
}
MemCtrl::~MemCtrl()
{
if(p != NULL)
delete []p;
p = NULL;
}
MemCtrl::MemCtrl(int size)
{
p = new int[size];
}
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#define _CRTDBG_MAP_ALLOC
#include<stdlib.h>
#include<crtdbg.h>
//#include "debug_new.h"
void Merge(int* In,int* Out,int i,int m,int n)
{//将有序的In[i..m]和In[m+1..n]归并为有序的Out[i..n]
int j,k;
for(j=m+1,k=i;i<=m && j<=n;++k)
{
if(In[i] < In[j])
Out[k] = In[i++];
else
Out[k] = In[j++];
}
// if(i<=m) Out[k..n] = In[i..m];
if(i<=m)
{
while(k<=n)
{
Out[k] = In[i];
k++;
i++;
}
}
// if(j<=n) Out[k..n] = In[j..n];
if(j<=n)
{
while(k<=n)
{
Out[k] = In[j];
k++;
j++;
}
}
}
int* MSort(int* In,int* Out1,int s,int t)
{
int m;
int* Out2 = NULL;
int *tmp1=NULL, *tmp2=NULL; //增加两个临时指针变量用于接收递归调用的返回值
Out2 = (int*)malloc(t*sizeof(int));
if(Out2 == NULL)
return Out2; //分配内存失败
if(s == t)
Out1[s] = In[s];
else
{
m = (s+t)/2;
tmp1=MSort(In,Out2,s,m); //接收下一级递归返回来的地址
tmp2=MSort(In,Out2,m+1,t);
Merge(Out2,Out1,s,m,t);
}
if(tmp1!=NULL) //判断是否分配了内存
free(tmp1); //释下一级递归放返回来的空间
if(tmp2!=NULL)
free(tmp2);
return Out2; //递归调用最开始一级的OUT2就要在主函数中去释放了。
}
int main(int argc, char* argv[])
{
_CrtMemState s1, s2, s3; // 声明内存状态结构变量
_CrtMemCheckpoint( &s1 );// 获取第一个内存状态快照
int in[]= {2,6,45,7,79,32,5,12,15,25};
int out[10];
MSort(in,out,0,9);
for(int i=0;i<10;i++)
printf("%d ",out[i]);
printf("\n");
free(out);
_CrtMemCheckpoint( &s2 );// 获取第二个内存状态快照
// 比较两个内存快照的差异
if ( _CrtMemDifference( &s3, &s1, &s2) )
_CrtMemDumpStatistics( &s3 );// dump 差异结果
// 输出内存泄漏报告,显示在 Output 窗口的 Debug 页中
_CrtDumpMemoryLeaks();
return 0;
}