标签:blog io ar for art div log c amp
自己写的
#include<stdio.h> void Merger(int num[],int start,int end) { int k,i,j,mid=(start+end)/2; int leftlen=mid-start+1,rightlen=end-mid; int totallen=end-start+1; int *total=new int[totallen]; // int *left=new int[leftlen]; //int *right=new int[rightlen]; // for(i=start,j=0;i<=mid;i++) // left[j++]=num[i]; //for(i=mid+1,j=0;i<=end;i++) //right[j++]=num[i]; for(k=0,i=start,j=mid+1;i<=mid && j<=end;) { if(num[i]<num[j]) { total[k]=num[i]; k++; i++; } else { total[k]=num[j]; k++; j++; } } while(i<=mid) total[k++]=num[i++]; while(j<=end) total[k++]=num[j++]; for(k=1,i=start;k<=totallen;k++,i++) num[i]=total[k-1]; delete []total; } void MeSort(int num[],int start,int end) { if(start==end) return; int mid=(start+end)/2; MeSort(num,start,mid); MeSort(num,mid+1,end); Merger(num,start,end); } void main() { int num[]={72,18,53,36,48,31,40}; int len=sizeof(num)/sizeof(int); int k; MeSort(num,0,len-1); for(k=0;k<=len-1;k++) printf("%d ",num[k]); }
标签:blog io ar for art div log c amp
原文地址:http://www.cnblogs.com/notlate/p/3961872.html