//将有序数组a[]和b[]合并到c[]中 void MemeryArray(int *a, int n, int *b, int m, int *c) { int x,y,z; x=y=z=0; while (x<n && y<m) { if (a[x] < b[y]) c[z++] = a[x++]; else c[z++] = b[y++]; } while (x < n) c[z++] = a[x++]; while (y < m) c[z++] = b[y++]; }
#include <stdio.h> #include <stdlib.h> void MergeArray(int *a,int left,int mid,int right) { int temp[100000]; int x,y,z; x=left,y=mid+1,z=0; while(x<=mid && y<=right) { if(a[x]<a[y]) temp[z++]=a[x++]; else temp[z++]=a[y++]; } while(x<=mid) temp[z++]=a[x++]; while(y<=right) temp[z++]=a[y++]; for(x=0;x<z;x++) a[left+x]=temp[x]; } void MergeSort(int *a,int left,int right) { int mid; if(left<right) { mid=(left+right)/2; MergeSort(a,left,mid);//将右边排序 MergeSort(a,mid+1,right);//将左边排序 MergeArray(a,left,mid,right);//将左边右边合并 } } int main() { int a[]={8,3,6,2,5,7,9,1,4,0}; MergeSort(a,0,9); for(int i=0;i<10;i++) printf("%d ",a[i]); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/baidu_30541191/article/details/47700877