合并排序
#include <iostream> using namespace std; const int N=1005; int newArray[N]; int a[N]; void Merge(int a[],int left,int mid,int right) { int i=left; int j=mid+1; int k=0; while(i<=mid&&j<=right){ if(a[i]<a[j]) newArray[k++]=a[i++]; else newArray[k++]=a[j++]; } while(i<=mid) newArray[k++]=a[i++]; while(j<=right) newArray[k++]=a[j++]; for(i=left;i<=right;i++) a[i]=newArray[i-left]; } void MergeSort(int a[], int left, int right) { if(left<right){ int mid=(left+right)>>1; MergeSort(a,left,mid); MergeSort(a,mid+1,right); Merge(a,left,mid,right); } } int main() { int a[]={2,43,4,5,23,454,65,7,100,3242}; int n=sizeof(a)/sizeof(a[0]); MergeSort(a,0,n-1); for(int i=0;i<n;i++) cout<<a[i]<<endl; return 0; }