Exemplo n.º 1
0
List *List_merge_sort(List *list, List_compare cmp)
{
	assert(list != NULL && "list can't be NULL");
	assert(cmp != NULL && "cmp can't be NULL");

	if(List_count(list) <= SUB_LIST_MIN_SIZE) {
		int rc = List_bubble_sort(list, cmp);

		assert(rc == 0 && "Bubble sort failed.");

		return list;
	}

	List *left = List_create();
	List *right = List_create();

	int middle = List_count(list) / 2;

	List_split(list, left, middle, right, List_count(list) - middle);

	List *sort_left = List_merge_sort(left, cmp);
	List *sort_right = List_merge_sort(right, cmp);

	if(sort_left != left) List_clear_destroy(left);
	if(sort_right != right) List_clear_destroy(right);
	
	List *merged_list = List_merge(sort_left, sort_right, cmp);

	List_clear_destroy(sort_left);
	List_clear_destroy(sort_right);

	return merged_list;
}
Exemplo n.º 2
0
List *List_merge_sort(List *list, List_compare cmp){
	if(List_count(list)<=1){
		return list;
	}
	
	List *left=List_create();
	List *right=List_create();
	int middle=List_count(list)/2;
	
	LIST_FOREACH(list, first, next, cur){
		if(middle>0){
			List_push(left, cur->value);
		} else {
			List_push(right, cur->value);
		}
		
		middle--;
	}
	
	List *sort_left=List_merge_sort(left, cmp);
	List *sort_right=List_merge_sort(right, cmp);
	
	if(sort_left !=left) List_destroy(left);
	if(sort_right !=right)List_destroy(right);
	
	return List_merge(sort_left, sort_right, cmp);
}
Exemplo n.º 3
0
List *List_merge_sort(List *list, List_compare cmp)
{
	// 未初始化的List,视为不能排序
	if(list == NULL) return NULL;
	// 空List和只有一个节点的List视为已经排序
	if(List_count(list) < 2) return list;

	int i = 1;
	ListNode *cur = list->first;
	List *left = List_create();
	List *right= List_create();
	int middle = List_count(list) / 2;
	// 拆成两个List,分别排序
	for(i = 1; i < middle; i++)
	{
		List_push(left, cur->value);
		cur=cur->next;
	}
	for(i = 1; i <= List_count(list) - middle; i++)
	{
		List_push(right, cur->value);
		cur=cur->next;
	}

	List *sort_left = List_merge_sort(left, cmp);
	List *sort_right =	List_merge_sort(right, cmp);

	if(sort_left != left) List_destroy(left);
	if(sort_right != right) List_destroy(right);
	

	// merge
	return List_merge(sort_left, sort_right, cmp);

}
Exemplo n.º 4
0
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 ;

}
Exemplo n.º 5
0
void		List_merge_sort(List *l, list_cmp_fct cmpf)
{
	List *out1;
	List *out2;

	if (!l || COUNT(l) <= 1)
		return ;

	List_cut_half(l, &out1, &out2);
	
	List_merge_sort(out1, cmpf);
	List_merge_sort(out2, cmpf);

	merge(out1, out2, l, cmpf);
}
Exemplo n.º 6
0
char *test_merge_sort()
{
	List *words = create_words();

	// should work on a list that needs sorting
	List *res = List_merge_sort(words, (List_compare)strcmp);
	mu_assert(is_sorted(res), "Words are not sorted after merge sort");

	List *res2 = List_merge_sort(res, (List_compare)strcmp);
	mu_assert(is_sorted(res2), "should still be sort after merge sort");
	
	List_destroy(res2);
	List_destroy(res);
	List_destroy(words);

	return NULL;
}
Exemplo n.º 7
0
List *List_merge_sort(List *list, List_compare *cmp) {
    if (list->count == 1) {
        List *res = List_create();
        List_push(res, list->first->value);
        return res;
    }

    int middle = list->count / 2;
    List *l= List_sublist(list, 0, middle);
    List *r= List_sublist(list, middle, list->count - middle);

    List *left = List_merge_sort(l, cmp);
    List *right = List_merge_sort(r, cmp);
    List_destroy(l);
    List_destroy(r);

    ListNode* leftNode = left->first;
    ListNode* rightNode = right->first;

    List *res = List_create();
    while (leftNode != NULL || rightNode != NULL) {
        if (leftNode == NULL) {
            List_push(res, rightNode->value);
            rightNode = rightNode->next;
        } else if (rightNode == NULL) {
            List_push(res, leftNode->value);
            leftNode = leftNode->next;
        } else if (cmp(leftNode->value, rightNode->value) < 0) {
            List_push(res, leftNode->value);
            leftNode = leftNode->next;
        } else {
            List_push(res, rightNode->value);
            rightNode = rightNode->next;
        }
    }

    List_destroy(left);
    List_destroy(right);

	return res;
}
Exemplo n.º 8
0
List *List_merge_sort(List *list, List_compare cmp) {
	if(List_count(list) <= 1) {
		return list;
	}
	List *left = List_create();
	List *right = List_create();
	int index = 0;

	LIST_FOREACH(list, first, next, cur) {
		if(index%2 == 1) {
			List_push(left, cur->value);
		} else {
			List_push(right, cur->value);
		}
		++index;
	}

	left = List_merge_sort(left, cmp);
	right = List_merge_sort(right, cmp);

	return List_merge(left, right, cmp);
}