归并排序

xiaoxiao2021-02-28  77

#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++]; else c[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; }
转载请注明原文地址: https://www.6miu.com/read-77540.html

最新回复(0)