示例#1
0
文件: heap.c 项目: gerard/algos
void minheap_build(int *v, int len)
{
    int i;

    PERF_START();
    PERF_PROBLEM_SIZE(len);
    for (i = len; i > 0; i--) {
        minheap_heapify(v, i, len);
    }
    PERF_STOP();
}
示例#2
0
文件: heap.c 项目: gerard/algos
void minheap_heapify(int *v, int p, int len)
{
    int l = LEFT(p);
    int r = RIGHT(p);
    int *min_child;

    if (l > len) return;

    if (r > len) {
        min_child = &v[l];
    } else {
        min_child = v[l] < v[r] ? &v[l] : &v[r];
    }

    if (v[p] > *min_child) {
        swap(&v[p], min_child);
        if (min_child == &v[l]) minheap_heapify(v, l, len);
        else minheap_heapify(v, r, len);
    }
}
示例#3
0
文件: heap.c 项目: gerard/algos
int minheap_extract(int *v, int *len)
{
    int ret = v[1];

    PERF_START();
    PERF_PROBLEM_SIZE(*len);
    v[1] = v[*len];
    (*len)--;
    minheap_heapify(v, 1, *len);
    PERF_STOP();

    return ret;
}
int main(void)
{
	uint32_t cnt, i, data[] =
	                    {14, 2, 22, 13, 23, 10, 90, 36, 108, 12,
	                      9, 91, 1, 51, 11, 3, 15, 80, 3, 78, 53,
	                      5, 12, 21, 65, 70, 4};
	const uint32_t data_len = sizeof(data)/sizeof(uint32_t);
	struct heap heap = heap_create(data_len + 5);

	printf(COL_BEG "push data...\n" COL_END);
	for (i = 0; i < data_len; i++)
		if (!heap_full(&heap))
			heap_push(&heap, &data[i]);

	printf("data len = %u, heap size = %u.\n", data_len,
	       heap_size(&heap));

	printf(COL_BEG "heap tree:\n" COL_END);
	heap_print_tr(&heap, &test_print);

	printf(COL_BEG "heap array:\n" COL_END);
	heap_print_arr(&heap, &test_print);

	heap_set_callbk(&heap, &test_less_than);

	printf(COL_BEG "after heapify:\n" COL_END);
	minheap_heapify(&heap);
	heap_print_tr(&heap, &test_print);

	cnt = 0;
	printf(COL_BEG "ranking emulation...:\n" COL_END);
	while (cnt < 100) {
		i = (i + 1) % data_len;
		if (!heap_full(&heap)) {
			printf("insert %d\n", data[i]);
			minheap_insert(&heap, &data[i]);
		} else {
			void *top = heap_top(&heap);
			if (test_less_than(top, &data[i])) {
				printf("replace with %d\n", data[i]);
				minheap_delete(&heap, 0);
				minheap_insert(&heap, &data[i]);
			}
		}
		cnt ++;
	}

	printf(COL_BEG "a heavy heap tree now:\n" COL_END);
	heap_print_tr(&heap, &test_print);

	minheap_sort(&heap);
	printf(COL_BEG "heap array after min-heap sort:\n" COL_END);
	heap_print_arr(&heap, &test_print);

	heap_destory(&heap);

	printf(COL_BEG "a new heap...\n" COL_END);
	heap = heap_create(data_len + 5);
	heap_set_callbk(&heap, &test_less_than);

	for (i = 0; i < data_len; i++)
		if (!heap_full(&heap))
			heap_push(&heap, &data[i]);

	printf(COL_BEG "heap array:\n" COL_END);
	heap_print_arr(&heap, &test_print);

	heap_sort_desc(&heap);
	printf(COL_BEG "heap array after heap sort:\n" COL_END);
	heap_print_arr(&heap, &test_print);

	heap_print_tr(&heap, &test_print);
	heap_destory(&heap);

	printf(COL_BEG "sort a heap with one element...\n" COL_END);

	heap = heap_create(data_len + 5);
	heap_set_callbk(&heap, &test_less_than);
	heap_push(&heap, &data[0]);
	heap_print_tr(&heap, &test_print);
	heap_sort_desc(&heap);

	heap_destory(&heap);
	return 0;
}