归并排序

来源:互联网 发布:stm32 socket编程 编辑:程序博客网 时间:2024/06/06 01:18
#include<stdio.h>#include<stdlib.h>void GuiBing(int a[],int first,int mid,int last,int c[]){int i=first;int j=mid+1;int m=mid;int n=last;int k=0;while((i<=m)&&(j<=n)){if(a[i]<=a[j])c[k++]=a[i++];elsec[k++]=a[j++];}while(i<=m)c[k++]=a[i++];while(j<=n)c[k++]=a[j++];for(int l=0;l<k;l++)a[first+l]=c[l];}void Divide(int a[],int first,int last,int c[]){if(first<last){int mid=(first+last)/2;Divide(a,first,mid,c);Divide(a,mid+1,last,c);GuiBing(a,first,mid,last,c);}}int main(){int paixu[]={10,54,2,59,6,325,489,0,266,12,32,88};int temp[12];Divide(paixu,0,11,temp);for(int i=0;i<12;i++)printf("%d,",temp[i]);return 0;}