求用归并法对数组排序的程序
希望有大佬可以给一个用归并法对数组排序的程序,谢谢
程序代码:/*****************************************************
File name:Quicksort
Description: 对数组进行归并排序
Funcion List: 实现快速排序算法
*****************************************************/
#include <stdio.h>
#include <stdlib.h>
void Swap(int a[],int i,int j)
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
void MergerSorting(int a[],int des[],int low,int mid,int high)
{
int i=low;
int j=mid+1;
int k=low;
while((i<=mid)&&(j<=high))
{
if(a[i]<a[j]) des[k++]=a[i++];
else des[k++]=a[j++];
}
while(i<=mid) des[k++]=a[i++];
while(j<=high) des[k++]=a[j++];
}
void MSorting(int a[],int des[],int low,int high,int max)
{
if(low==high)
{
des[low]=a[low];
}
else
{
int mid=(low+high)/2;
int* space=(int*)malloc(sizeof(int)*max);
if(space!=NULL)
{
MSorting(a,space,low,mid,max);
MSorting(a,space,mid+1,high,max);
MergerSorting(space,des,low,mid,high);
free(space);
}
}
}
void MerSorting(int a[],int len)
{
MSorting(a,a,0,len-1,len);
}
void ShowSorting(int a[], int len)
{
for(int i=0;i<len;i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
int main()
{
int a[]={1,5,21,20,31,2,15,100};
int len=sizeof(a)/sizeof(*a);
//BubbleSorting(a,len);
//SelectSorting(a,len);
//InsertSorting(a,len);
//ShellSorting(a,len);
//QuickSorting(a,len);
MerSorting(a,len);
ShowSorting(a,len);
return 0;
}
~
~
程序代码:
#include<stdio.h>
#include<stdarg.h>
#include<time.h>
void init_srand( long* );
void test1( size_t,... );
void test2( size_t,unsigned );
void test3( unsigned,unsigned,unsigned );
void check( int*,size_t );
void sort( int*,size_t );
static void _sort( int*,size_t,size_t );
static void _merge( int*,int*,int* );
void print( const int [],size_t size );
int main( void )
{
size_t i;
time_t start;
init_srand(NULL);
start=time(NULL);
test1(1,0); //测试1个数
test1(2,1,2); //测试顺序两个数
test1(2,2,1); //测试逆序两个数
test1(5,1,1,1,1,1); //测试输入重复数
test1(5,1,2,3,4,5); //测试顺序5个数
test1(5,5,4,3,2,1); //测试逆序5个数
test1(5,1,2,1,1,2); //测试重复数
test1(5,3,2,1,5,4); //测试一般序列
test1(5,-3,-2,-1,-5,-4); //测试负数
puts("ACROSS TEST1");
for (i=1;i!=10001;++i) //从1个数据到10000个数据每个数据段覆盖1次
test2(i,1);
puts("ACROSS TEST2");
test3(1,1000000,10); //随机产生1~1000000元素个数,测试10次
puts("ACROSS TEST3");
printf("The test of time is %g s\n",difftime(time(NULL),start));
getchar();
return 0;
}
#include<stdlib.h>
void init_srand( long* data )
{
srand(( unsigned )time(data));
}
#include<string.h>
#include<assert.h>
void test1( size_t len,... )
{
va_list ap;
int* a=( int* )malloc(len*sizeof (*a));
size_t i;
assert(a);
va_start(ap,len);
for (i=0;i!=len;++i)
a[i]=va_arg(ap,int);
va_end(ap);
sort(a,len);
check(a,len); //检查数据是否存在bug
free(a);
}
void test2( size_t len,unsigned count )
{
int* buf=( int* )malloc(len*sizeof (*buf));
assert(buf);
do
{
size_t i;
for (i=0;i!=len;++i)
buf[i]=rand()%100;
sort(buf,len);
check(buf,len); //检查数据是否存在bug
}while (--count);
free(buf);
}
void test3( unsigned low,unsigned height,unsigned count )
{
size_t i;
if (low>height)
{
fprintf(stderr,"TEST3 DATA RANGE ERROR\n");
exit(EXIT_FAILURE);
}
for (i=0;i!=count;++i)
{
const size_t len=rand()%(height-low+1)+low;
test2(len,1);
}
}
void check( int* a,size_t len )
{
size_t i;
if (len<2)
return ;
for (i=0;i!=len-1;++i)
assert(a[i]<=a[i+1]);
}
void sort( int* a,size_t size )
{
if (size==0)
return ;
_sort(a,0,size);
}
static void _sort( int* a,size_t low,size_t height )
{
if (low>=height-1)
return ;
{
const size_t mid=(low+height)/2;
_sort(a,low,mid);
_sort(a,mid,height);
_merge(a+low,a+mid,a+height);
}
}
#include<string.h>
static void _merge( int* p,int* q,int* p_height )
{
const size_t len=p_height-p;
const int* const p_mid=q;
int* buf=( int* )malloc(len*sizeof (*buf));
int* p_buf=buf;
assert(buf);
while (p!=p_mid&&q!=p_height)
*(p_buf++)=*p<*q?*(p++):*(q++);
if (p!=p_mid)
memcpy(p_buf,p,(p_mid-p)*sizeof (*p));
else
memcpy(p_buf,q,(p_height-q)*sizeof (*q));
memcpy(p_height-len,buf,len*sizeof (*buf));
free(buf);
}
void print( const int a[],size_t size )
{
size_t i;
for (i=0;i!=size;++i)
printf("%-4d",a[i]);
puts("");
}[此贴子已经被作者于2018-5-19 23:43编辑过]

~

~
程序代码:
#include<stdio.h>
#include<stdarg.h>
void init_srand( long* );
void test1( size_t,... );
void test2( size_t,unsigned );
void check( int*,size_t );
void sort( int*,size_t );
static unsigned _sort( int*,int*,size_t,size_t );
static void _merge( int*,int*,int*,const int* );
void print( const int [],size_t size );
int main( void )
{
size_t i;
test1(1,0); //测试1个数
test1(2,1,2); //测试顺序两个数
test1(2,2,1); //测试逆序两个数
test1(5,1,1,1,1,1); //测试输入重复数
test1(5,1,2,3,4,5); //测试顺序5个数
test1(5,5,4,3,2,1); //测试逆序5个数
test1(5,1,2,1,1,2); //测试重复数
test1(5,3,2,1,5,4); //测试一般序列
test1(5,-3,-2,-1,-5,-4); //测试负数
puts("");
init_srand(NULL);
for (i=1;i!=64;++i) //从1个数据到64个数据每个数据段覆盖1次
test2(i,1);
return 0;
}
#include<stdlib.h>
#include<time.h>
void init_srand( long* data )
{
srand(( unsigned )time(data));
}
#include<string.h>
#include<assert.h>
void test1( size_t len,... )
{
va_list ap;
int* buf=( int* )malloc(len*sizeof (*buf));
size_t i;
assert(buf);
va_start(ap,len);
for (i=0;i!=len;++i)
buf[i]=va_arg(ap,int);
va_end(ap);
sort(buf,len);
print(buf,len);
check(buf,len); //检查数据是否存在bug
free(buf);
}
void test2( size_t len,unsigned count )
{
int* buf=( int* )malloc(len*sizeof (*buf));
assert(buf);
do
{
size_t i;
for (i=0;i!=len;++i)
buf[i]=rand()%100;
sort(buf,len);
print(buf,len);
check(buf,len); //检查数据是否存在bug
}while (--count);
puts("");
free(buf);
}
void check( int* a,size_t len )
{
size_t i;
if (len<2)
return ;
for (i=0;i!=len-1;++i)
assert(a[i]<=a[i+1]);
}
void sort( int* a,size_t size )
{
int* buf=NULL;
if (size==0)
return ;
buf=( int* )malloc(size*sizeof (*buf));
assert(buf);
memset(buf,0,size*sizeof (*buf));
if (_sort(a,buf,0,size)&1)
memcpy(a,buf,size*sizeof (*buf));
free(buf);
}
static unsigned _sort( int* a,int* buf,size_t low,size_t height )
{
const size_t mid=(low+height)/2;
unsigned res=1;
if (low>=height-1)
{
*(buf+low)=*(a+low);
return 0;
}
if (height-mid>1)
{
_sort(a,buf,low,mid);
res=_sort(a,buf,mid,height)+1;
}
else
{
_merge(buf+low,a+low,a+mid,a+height);
_merge(a+low,buf+low,buf+mid,buf+height);
return res;
}
if (((res&1)!=0)&&(res!=5))
_merge(buf+low,a+low,a+mid,a+height);
else if (((res&1)==0)&&(res!=2)&&(res!=4))
_merge(a+low,buf+low,buf+mid,buf+height);
else
{
_merge(a+low,buf+low,buf+mid,buf+height);
_merge(buf+low,a+low,a+mid,a+height);
}
return res;
}
static void _merge( int* buf,int* p,int* q,const int* p_height )
{
const size_t len=p_height-p;
const int* const p_mid=q;
int* p_buf=buf;
while ((p!=p_mid)&&(q!=p_height))
*(p_buf++)=*p<*q?*(p++):*(q++);
if (p!=p_mid)
memcpy(p_buf,p,(p_mid-p)*sizeof (*p));
else
memcpy(p_buf,q,(p_height-q)*sizeof (*q));
}
void print( const int a[],size_t size )
{
size_t i;
for (i=0;i!=size;++i)
printf("%-4d",a[i]);
puts("");
}
[此贴子已经被作者于2018-5-19 22:27编辑过]

~
程序代码:
#include<stdio.h>
#include<stdarg.h>
#include<time.h>
void init_srand( long* );
void test1( size_t,... );
void test2( size_t,unsigned );
void test3( unsigned,unsigned,unsigned );
void check( int*,size_t );
void sort( int*,size_t );
static void _sort( int*,int*,size_t,size_t,unsigned );
static void _merge( int*,const int*,const int*,const int* );
void print( const int [],size_t size );
int main( void )
{
size_t i;
time_t start;
init_srand(NULL);
start=time(NULL);
test1(1,0); //测试1个数
test1(2,1,2); //测试顺序两个数
test1(2,2,1); //测试逆序两个数
test1(5,1,1,1,1,1); //测试输入重复数
test1(5,1,2,3,4,5); //测试顺序5个数
test1(5,5,4,3,2,1); //测试逆序5个数
test1(5,1,2,1,1,2); //测试重复数
test1(5,3,2,1,5,4); //测试一般序列
test1(5,-3,-2,-1,-5,-4); //测试负数
puts("ACROSS TEST1");
for (i=1;i!=10001;++i) //从1个数据到10000个数据每个数据段覆盖1次
test2(i,1);
puts("ACROSS TEST2");
test3(1,1000000,10); //随机产生1~1000000元素个数,测试10次
puts("ACROSS TEST3");
printf("The test of time is %g s\n",difftime(time(NULL),start));
getchar();
return 0;
}
#include<stdlib.h>
void init_srand( long* data )
{
srand(( unsigned )time(data));
}
#include<string.h>
#include<assert.h>
void test1( size_t len,... )
{
va_list ap;
int* a=( int* )malloc(len*sizeof (*a));
size_t i;
assert(a);
va_start(ap,len);
for (i=0;i!=len;++i)
a[i]=va_arg(ap,int);
va_end(ap);
sort(a,len);
check(a,len); //检查数据是否存在bug
free(a);
}
void test2( size_t len,unsigned count )
{
int* buf=( int* )malloc(len*sizeof (*buf));
assert(buf);
do
{
size_t i;
for (i=0;i!=len;++i)
buf[i]=rand()%100;
sort(buf,len);
check(buf,len); //检查数据是否存在bug
}while (--count);
free(buf);
}
void test3( unsigned low,unsigned height,unsigned count )
{
size_t i;
if (low>height)
{
fprintf(stderr,"TEST3 DATA RANGE ERROR\n");
exit(EXIT_FAILURE);
}
for (i=0;i!=count;++i)
{
const size_t len=rand()%(height-low+1)+low;
test2(len,1);
}
}
void check( int* a,size_t len )
{
size_t i;
if (len<2)
return ;
for (i=0;i!=len-1;++i)
assert(a[i]<=a[i+1]);
}
void sort( int* a,size_t size )
{
int* buf=NULL;
if (size==0)
return ;
buf=( int* )malloc(size*sizeof (*buf));
assert(buf);
memcpy(buf,a,size*sizeof (*buf));
_sort(a,buf,0,size,1);
free(buf);
}
static void _sort( int* a,int* buf,size_t low,size_t height,unsigned deep )
{
const size_t mid=(low+height)/2;
if (low>=height-1)
return ;
_sort(a,buf,low,mid,deep+1);
_sort(a,buf,mid,height,deep+1);
if (deep&1)
_merge(a+low,buf+low,buf+mid,buf+height);
else
_merge(buf+low,a+low,a+mid,a+height);
}
static void _merge( int* buf,const int* p,const int* q,const int* p_height )
{
const int* const p_mid=q;
int* p_buf=buf;
while ((p!=p_mid)&&(q!=p_height))
*(p_buf++)=*p<*q?*(p++):*(q++);
if (p!=p_mid)
memcpy(p_buf,p,(p_mid-p)*sizeof (*p));
else
memcpy(p_buf,q,(p_height-q)*sizeof (*q));
}
void print( const int a[],size_t size )
{
size_t i;
for (i=0;i!=size;++i)
printf("%-4d",a[i]);
puts("");
}
~[此贴子已经被作者于2018-5-20 02:59编辑过]

~
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<stdarg.h>
#include<time.h>
typedef void (*SORT_FUN)( void*,size_t,size_t,int (*)( const void*,const void* ) );
void sort_test( SORT_FUN );
int comp( const void*,const void* ); //比较函数
void init_srand( long* );
void test1( size_t,SORT_FUN,... );
void test2( size_t,unsigned,SORT_FUN );
void test3( unsigned,unsigned,unsigned,SORT_FUN );
void check( int*,size_t );
void merge_sort( void*,size_t,size_t,int (*)( const void*,const void* ) );
static void _merge( char*,const char*,const char*,const char*,size_t,\
int (*)( const void*,const void* ));
void print( const int [],size_t size );
int main( void )
{
puts("Test qsort:\n");
sort_test(qsort);
puts("\nTest merge_sort:\n");
sort_test(merge_sort);
getchar();
return 0;
}
int comp( const void* p,const void* q )
{
return *( int* )p-*( int* )q;
}
void sort_test( SORT_FUN sort_fun )
{
size_t i;
time_t start;
init_srand(NULL);
start=time(NULL);
test1(1,sort_fun,0); //测试1个数
test1(2,sort_fun,1,2); //测试顺序两个数
test1(2,sort_fun,2,1); //测试逆序两个数
test1(5,sort_fun,1,1,1,1,1); //测试输入重复数
test1(5,sort_fun,1,2,3,4,5); //测试顺序5个数
test1(5,sort_fun,5,4,3,2,1); //测试逆序5个数
test1(5,sort_fun,1,2,1,1,2); //测试重复数
test1(5,sort_fun,3,2,1,5,4); //测试一般序列
test1(5,sort_fun,-3,-2,-1,-5,-4); //测试负数
puts("ACROSS TEST1");
for (i=1;i!=10001;++i) //从1个数据到10000个数据每个数据段覆盖1次
test2(i,1,sort_fun);
puts("ACROSS TEST2");
test3(1,1000000,10,sort_fun); //随机产生1~1000000元素个数,测试10次
puts("ACROSS TEST3");
printf("The test of time is %g s\n",difftime(time(NULL),start));
}
void init_srand( long* data )
{
srand(( unsigned )time(data));
}
#include<string.h>
#include<assert.h>
void test1( size_t len,SORT_FUN sort_fun,... )
{
va_list ap;
int* a=( int* )malloc(len*sizeof (*a));
size_t i;
assert(a);
va_start(ap,sort_fun);
for (i=0;i!=len;++i)
a[i]=va_arg(ap,int);
va_end(ap);
sort_fun(a,len,sizeof (int),comp);
check(a,len); //检查数据是否存在bug
free(a);
}
void test2( size_t len,unsigned count,SORT_FUN sort_fun )
{
int* buf=( int* )malloc(len*sizeof (*buf));
assert(buf);
do
{
size_t i;
for (i=0;i!=len;++i)
buf[i]=rand()%100;
sort_fun(buf,len,sizeof (int),comp);
check(buf,len); //检查数据是否存在bug
}while (--count);
free(buf);
}
void test3( unsigned low,unsigned height,unsigned count,SORT_FUN sort_fun )
{
size_t i;
if (low>height)
{
fprintf(stderr,"TEST3 DATA RANGE ERROR\n");
exit(EXIT_FAILURE);
}
for (i=0;i!=count;++i)
{
const size_t len=rand()%(height-low+1)+low;
test2(len,1,sort_fun);
}
}
void check( int* a,size_t len )
{
size_t i;
if (len<2)
return ;
for (i=0;i!=len-1;++i)
assert(a[i]<=a[i+1]);
}
typedef struct Stack
{
size_t low;
size_t height;
unsigned deep;
int flag;
}Stack,*P_Stack;
#include<math.h>
void merge_sort( void* _a,size_t size,size_t n_size,int (*comp)( const void*,const void* ) )
{
char* buf=NULL;
char* a=( char* )_a;
P_Stack stack=NULL;
P_Stack base=NULL;
P_Stack top=NULL;
if (size==0)
return ;
buf=( char* )malloc(size*n_size);
assert(buf);
memcpy(buf,a,size*n_size);
base=stack=( P_Stack )malloc(( size_t )(log(size)+10)*sizeof (Stack));
assert(stack);
stack->flag=0;
top=base+1;
top->low=0;
top->height=size;
top->deep=1;
top->flag=0;
#define __STACK_PUSH( top,_low,_height ) \
do \
{ \
((top)+1)->low=(_low); \
((top)+1)->height=(_height); \
((top)+1)->deep=(top)->deep+1; \
((top)+1)->flag=0; \
++(top); \
}while (0);
#define __STACK_POP( top ) \
do \
{ \
--(top); \
++(top)->flag; \
}while (0);
while (top!=base)
{
const size_t low=top->low;
const size_t mid=(top->low+top->height)/2;
const size_t height=top->height;
if (low==height-1)
{
__STACK_POP(top);
continue;
}
if (top->flag==0)
{
__STACK_PUSH(top,low,mid);
continue;
}
if (top->flag==1)
{
__STACK_PUSH(top,mid,height);
continue;
}
if (top->deep&1)
_merge(a+low*n_size,buf+low*n_size,buf+mid*n_size,buf+height*n_size,n_size,comp);
else
_merge(buf+low*n_size,a+low*n_size,a+mid*n_size,a+height*n_size,n_size,comp);
__STACK_POP(top);
}
free(buf);
#undef __STACK_PUSH
#undef __STACK_POP
}
static void _merge( char* buf,const char* p,const char* q,const char* p_height,size_t n_size,\
int (*comp)( const void*,const void* ) )
{
const char* const p_mid=q;
char* p_buf=buf;
while ((p!=p_mid)&&(q!=p_height))
if (comp(p,q)>0)
{
memcpy(p_buf,q,n_size);
p_buf+=n_size;
q+=n_size;
}
else
{
memcpy(p_buf,p,n_size);
p_buf+=n_size;
p+=n_size;
}
if (p!=p_mid)
memcpy(p_buf,p,p_mid-p);
else
memcpy(p_buf,q,p_height-q);
}
void print( const int a[],size_t size )
{
size_t i;
for (i=0;i!=size;++i)
printf("%-4d",a[i]);
puts("");
}
~[此贴子已经被作者于2018-5-20 14:13编辑过]
