首页 > 代码库 > Merge Sort

Merge Sort

技术分享
 1 #include<stdlib.h> 2 #include<stdio.h> 3 void Merge( int source[] , int temp[] , int start , int mid , int end ) 4 { 5     int i = start , j = mid + 1 , k = start ; 6     while ( i != mid + 1 && j != end + 1 ) 7     { 8         if( source[i] > source[j] ) 9             temp[k++] = source[i++];10         else11             temp[k++] = source[j++];12     }13     while( i != mid + 1 )14         temp[k++] = source[i++];15     while( j != end + 1 )16         temp[k++] = source[j++];17     for( i = start ; i <= end ; i++ )18         source[i] = temp[i];19 }   //ÄÚ²¿Ê¹Óõݹé20 void MergeSort( int source[] , int temp[] , int start , int end )21 {22     int mid;23     if( start < end )24     {25         mid = ( start + end ) / 2 ;26         MergeSort( source , temp , start , mid );27         MergeSort( source , temp , mid+1 , end );28         Merge( source , temp , start , mid , end );29     }30 }31 int main()32 {33     int a[]={18,78,244,36,9,12,4,5,68,7,4,895,321,50,10,30,98,74,56,13,25,46,789,420,159,360,5648,78,1,2,3,6,98};34     int i,b[1000];35     MergeSort( a , b , 0 , sizeof(a)/sizeof(a[0])-1 );36     for( i = 0 ; i < sizeof(a)/sizeof(a[0]) ; i++ )37         printf( "%d\t" , a[i] );38     printf("\n");39     return 0;40 }
Merge

 

Merge Sort