设为首页 - 加入收藏 焦点技术网
热搜:java
当前位置:首页 >

归并排序

2014-11-22 19:41:00.0 <<算法导论>>  
导读:#includeint a[1000],t[1000];void merge(int l,int r){ if(l < r) { int m = (l + r) >> 1; merge(l,m); merge(m+1,r); int i = l,left = l,right = m + 1; while(left <= m || right <= r) { if(right > r...。。。
#includeint a[1000],t[1000];void merge(int l,int r){ if(l < r) {  int m = (l + r) >> 1;  merge(l,m);  merge(m+1,r);  int i = l,left = l,right = m + 1;  while(left <= m || right <= r)  {   if(right > r || left <= m && a[left] <= a[right])     t[i++] = a[left++];   else     t[i++] = a[right++];  }  for(i = l;i <= r;i ++)    a[i] = t[i]; }}int main(){ int i,n; while(~scanf("%d",&n)) {  for(i = 1;i <= n;i ++)    scanf("%d",&a[i]);  merge(1,n);  for(i = 1;i < n;i ++)    printf("%d ",a[i]);  printf("%d\n",a[n]); } return 0;}

(编辑: jiezhitianyiya)

网友评论