#include <stdio.h> #include <stdlib.h> void mergesort(int arr[],int n,int m) { if(n>=m) { return; } mergesort(arr,n,(n+m)/2); mergesort(arr,(n+m)/2+1,m); int mid=(n+m)/2+1,i,k,index; int reg[m-n+1]; for(i=n,k=mid,index=0;i<=mid-1&&k<=m;) { reg[index++]=arr[i]<arr[k]? arr[i++]:arr[k++]; } while(i<=mid-1) { reg[index++]=arr[i++]; } while(k<=m) { reg[index++]=arr[k++]; } for(i=n;i<=m;i++) { arr[i]=reg[i-n]; } return; } int main() { int i,k; int arr[10]={13,36,78,9,53,78,100,66,10,14}; for(i=0;i<10;i++) printf("%d ",arr[i]); printf("\n"); mergesort(arr,0,9); for(i=0;i<10;i++) printf("%d ",arr[i]); return 0; }
沒有留言:
張貼留言