码迷,mamicode.com
首页 > 编程语言 > 详细

归并排序

时间:2015-12-02 17:48:17      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

#include <stdio.h>
#include <stdlib.h>
void merge(int r[],int first,int last,int mid)
{
    int number_temp[10]={0};
    int i=first,j=mid+1,k;
    for(k=0;k<=last-first;k++)
    {
     if (i==mid+1)
     {
      number_temp[k]=r[j++];
      continue;
     }
     if (j==last+1)
     {
      number_temp[k]=r[i++];
      continue;
     }
     if (r[i]<r[j])  number_temp[k]=r[i++];
     else  number_temp[k]=r[j++];
    }
    for (i=first,j=0;i<=last;i++,j++)
     r[i] = number_temp[j];
}
void merge_sort(int r[],int first,int last)
{
    int mid=0;
    if(first<last)
    {
     mid=(first+last)/2;
  merge_sort(r,first,mid);
  merge_sort(r,mid+1,last);
     merge(r,first,last,mid);
    }
}
int main()
{
    int r[10]={0},n,i;
    scanf("%d",&n);
    for(i=0;i<n;i++)
     scanf("%d",&r[i]);
    merge_sort(r,0,n-1);
    for(i=0;i<n;i++)
     printf("%d ",r[i]);
    system("pause");
    return 0;
}

归并排序

标签:

原文地址:http://www.cnblogs.com/100114jerro/p/4998748.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!