标签:
#include<stdio.h> #include<windows.h> int a[20]; int temp[20]; void combine(int begin,int n){ int head1 = begin; int head2 = begin+n/2; int index=begin; while(head1<begin+n/2&&head2<begin+n){ int maxn = a[head1]<a[head2]?head1++:head2++; temp[index++]=a[maxn]; } while(head1<begin+n/2){ temp[index++]=a[head1++]; } while(head2<begin+n){ temp[index++]=a[head2++]; } for(int i=begin;i<begin+n;i++){ a[i]=temp[i]; } } void combineSort(int begin,int n){ if(n==1){ return; } else{ combineSort(begin,n/2); combineSort(begin+n/2,n-n/2); combine(begin,n); } } int main(){ int n; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&a[i]); } combineSort(0,n); for(int i=0;i<n;i++){ printf("%d ",a[i]); } }
标签:
原文地址:http://www.cnblogs.com/lhppom/p/5463238.html