******** fig7.10 ********** /* l_pos = start of left half, r_pos = start of right half */ void merge( input_type a[], input_type tmp_array[], int l_pos, int r_pos, int right_end) { int i, left_end, num_elements, tmp_pos; left_end = r_pos - 1; tmp_pos = l_pos; num_elements = right_end - l_pos + 1; while( (l_pos <= left_end) && (r_pos <= right_end) ) /* main loop */ if( a[l_pos] <= a[r_pos] ) tmp_array[tmp_pos++] = a[l_pos++]; else tmp_array[tmp_pos++] = a[r_pos++]; while( l_pos <= left_end ) /* copy rest of first half */ tmp_array[tmp_pos++] = a[l_pos++]; while( r_pos <= right_end ) /* copy rest of second half */ tmp_array[tmp_pos++] = a[r_pos++]; for(i=1; i<=num_elements; i++, right_end-- ) /* copy tmp_array back */ a[right_end] = tmp_array[right_end]; }