1. Merge Sort (Iteration)
void merge(int bb, int mm, int ee){ int b = bb, k = bb, m = mm+1; while( b <= mm && m <= ee ) { if( A[ b ] <= A[ m ] ) S[ k++ ] = A[ b++ ]; else S[ k++ ] = A[ m++ ]; } if( b <= mm ) while( b <= mm ) S[ k++ ] = A[ b++ ]; else while( m <= ee ) S[ k++ ] = A[ m++ ]; for( int i = bb; i <= ee; i++) A[ i ] = S[ i ]; } void merge_sort(int bb, int ee) { #define min(a,b) ((a) < (b) ? (a) : (b)) for( int k = 1; k <= ee; k *= 2 ) { for ( int i = bb; i <= ee; i += k*2 ) { merge(i, min(i+k-1, ee), min(i+k*2-1, ee)); } } }
댓글 없음:
댓글 쓰기