标签:style blog color os io for div log sp
1 #include <iostream> 2 using namespace std; 3 int Merge(int A[],int p,int q,int r); 4 int Merge_Sort(int A[],int p,int r); 5 int OutPut(int A[],int n); 6 int main() 7 { 8 int A[]={2,3,5,4,1,6}; 9 Merge_Sort(A,1,6); 10 OutPut(A,6); 11 return 0; 12 } 13 14 int Merge(int A[],int p,int q,int r) 15 { 16 int n1=q-p+1; 17 int n2=r-q; 18 int * L=new int[n1]; 19 int * R=new int[n2]; 20 for(int i=0;i<n1;i++) 21 { 22 L[i]=A[p+i-1]; 23 } 24 for(int i=0;i<n2;i++) 25 { 26 R[i]=A[q+i]; 27 } 28 for(int i=0,j=0,k=p-1;k<r;k++) 29 { 30 if(L[i]<=R[j]&&i<n1||j==n2) 31 { 32 A[k]=L[i]; 33 i++; 34 } 35 else 36 { 37 A[k]=R[j]; 38 j++; 39 } 40 } 41 delete []L; 42 delete []R; 43 return 0; 44 } 45 46 int Merge_Sort(int A[],int p,int r) 47 { 48 if(p<r) 49 { 50 int q=(p+r)/2; 51 Merge_Sort(A,p,q); 52 Merge_Sort(A,q+1,r); 53 Merge(A,p,q,r); 54 } 55 return 0; 56 } 57 58 int OutPut(int A[],int n) 59 { 60 for(int i=0;i<n;i++) 61 { 62 cout<<A[i]<<‘\n‘; 63 } 64 return 0; 65 }
标签:style blog color os io for div log sp
原文地址:http://www.cnblogs.com/niuxiaoha/p/3951269.html