/*
归并排序
*/
#include <iostream>
#include <cstdio>
using namespace std;
void merg( int *a,int sta,int mid,int end )
{
int i,j,k;
int *b=new int[end-sta+1];
k=0 , i=sta , j=mid ;
while( i<mid && j<=end )
{
if( a[i]<a[j] ) b[k++]=a[i++];
else b[k++]=a[j++];
}
while( i<mid ) b[k++]=a[i++];
while( j<=end ) b[k++]=a[j++];
for( i=0;i<end-sta+1;i++ )
a[sta+i]=b[i];
delete b;
}
void mergsort( int *a,int sta,int end )
{
int i,mid;
if( sta==end ) return;
else
{
mid=( sta+end )/2;
mergsort( a,sta,mid );
mergsort( a,mid+1,end );
merg( a,sta,mid+1,end );
}
}
int main()
{
int i;
int a[10]={73,56,71,2,4,34,47,46,25,12};
mergsort( a,0,9 );
for( i=0;i<10;i++ )
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
归并排序
最新推荐文章于 2024-08-12 19:32:44 发布