标签:
#include <iostream> using namespace std; void print(int a[],int len) { for(int i=0;i<len;i++) cout<<a[i]<<" "; cout<<endl; } void merge(int a[],int l,int m,int h) { int i=0; int j=0; int k=0; int len=h-l+1; int *temp=new int [len]; for(i=l,j=m+1;i<=m&&j<=h;) { if(a[i]<a[j]) { temp[k++]=a[i++]; } else { temp[k++]=a[j++]; } } if(i<=m) for(;i<=m;i++,k++) temp[k]=a[i]; if(j<=h) for(;j<=h;j++,k++) temp[k]=a[j]; for(i=0;i<len;i++,l++) { a[l]=temp[i]; } cout<<"int merge:"<<endl; print(a,len); } void mergeSort(int a[],int l,int h) { if(l>=h) return; int m=(h+l)/2; mergeSort(a,l,m); mergeSort(a,m+1,h); merge(a,l,m,h); } int main() { int a[]={1,3,5,7,9,2,4,6,8,10,11,43,4,4,6,78,31,124}; // int a[2]={2,1}; int len=sizeof(a)/sizeof(int); print(a,len); // merge(a,0,4,9); mergeSort(a,0,len-1); print(a,len); getchar(); return 0; }
标签:
原文地址:http://www.cnblogs.com/lxdonge/p/4549498.html