List *List_merge_sort( List* words , List_compare cmp){ /* find middle , split into two lists * recursively do this ,until only one left * then recursively merge two lists */ int middle; static int i = 1; List *second; if( words->count == 0 || words->count == 1 ) return words; middle = words->count / 2; second = List_split( words , middle); assert( second != NULL ); /*after this words stores the first part before middle * and second begin with middle-th node */ words = List_merge_sort(words , cmp); second = List_merge_sort(second , cmp); // printf("times: %d\n" , i++); /* List_travers( words); List_travers( second); */ List *dest = merge_two_parts( words , second , cmp); log_info("now merge"); return dest ; }
void merge_sort_seq(Array<Item>& a, int_t left, int_t right, ResultArray<Item>& result, int_t result_offset, TmpArray<Item>& tmp_array, int_t tmp_offset, const Compare_fct& compare) { if (left == right) { return; } if (left + 1 == right) { result[result_offset] = a[left]; return; } int_t mid = (left + right) >> 1; merge_sort_seq(a, left, mid, result, result_offset, tmp_array, tmp_offset, compare); merge_sort_seq(a, mid, right, result, result_offset + mid - left, tmp_array, tmp_offset + mid - left, compare); merge_two_parts(result, left, mid, result, mid, right, tmp_array, tmp_offset, compare); std::copy(tmp_array.begin() + tmp_offset, tmp_array.begin() + tmp_offset + right - left, result.begin() + result_offset); }