|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <stdbool.h> |
| 4 | +#include <string.h> |
| 5 | +#include <time.h> |
| 6 | + |
| 7 | +/* Implement heap */ |
| 8 | + |
| 9 | +#define MAX_HEAP_SIZE (1 << 8) |
| 10 | + |
| 11 | +struct element { |
| 12 | + int data; |
| 13 | +}; |
| 14 | + |
| 15 | +struct heap { |
| 16 | + union { |
| 17 | + unsigned long elements; |
| 18 | + struct element *elem[MAX_HEAP_SIZE]; |
| 19 | + }; |
| 20 | +}; |
| 21 | + |
| 22 | +void init_heap(struct heap *heap) |
| 23 | +{ |
| 24 | + int i; |
| 25 | + |
| 26 | + for(i = 0; i < MAX_HEAP_SIZE; i++) { |
| 27 | + heap->elem[i] = NULL; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +void dump_heap(struct heap *heap, int index) |
| 32 | +{ |
| 33 | + struct element *elem; |
| 34 | + int level; |
| 35 | + |
| 36 | + if (index > heap->elements) |
| 37 | + return; |
| 38 | + |
| 39 | + elem = heap->elem[index]; |
| 40 | + level = fls(index); |
| 41 | + |
| 42 | + dump_heap(heap, index * 2 + 1); |
| 43 | + |
| 44 | + if (!(index % 2) && index != 1) |
| 45 | + printf("%*s\n", level*3, "|"); |
| 46 | + |
| 47 | + printf("%*s - %05d\n", level*3, " ", elem->data); |
| 48 | + |
| 49 | + if (index % 2 && index != 1) |
| 50 | + printf("%*s\n", level*3, "|"); |
| 51 | + |
| 52 | + dump_heap(heap, index * 2); |
| 53 | +} |
| 54 | + |
| 55 | +void dump(struct heap *heap, int elements) |
| 56 | +{ |
| 57 | + int i; |
| 58 | + |
| 59 | + for (i = 1; i <= elements; i++) |
| 60 | + printf("[%02d]: %4d\n", i, heap->elem[i]->data); |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +struct element* create_element(int data) |
| 65 | +{ |
| 66 | + struct element *elem; |
| 67 | + |
| 68 | + elem = malloc(sizeof(struct element)); |
| 69 | + |
| 70 | + if (elem) |
| 71 | + elem->data = data; |
| 72 | + |
| 73 | + return elem; |
| 74 | +} |
| 75 | + |
| 76 | +void fake_a_heap(struct heap *heap) |
| 77 | +{ |
| 78 | + /* data is in ordered */ |
| 79 | + int i, data[10] = {7, 4, 9, 2, 6, 8, 10, 1, 3, 5}; |
| 80 | + |
| 81 | + init_heap(heap); |
| 82 | + |
| 83 | + /* root start at 1 */ |
| 84 | + for (i = 0; i < 10; i++) |
| 85 | + heap->elem[i+1] = create_element(data[i]); |
| 86 | + |
| 87 | + heap->elements = 10; |
| 88 | +} |
| 89 | + |
| 90 | +void swap(struct heap *heap, int i, int j) |
| 91 | +{ |
| 92 | + struct element *tmp; |
| 93 | + |
| 94 | + tmp = heap->elem[j]; |
| 95 | + heap->elem[j] = heap->elem[i]; |
| 96 | + heap->elem[i] = tmp; |
| 97 | +} |
| 98 | + |
| 99 | +void heapify(struct heap *heap, int parent) |
| 100 | +{ |
| 101 | + struct element **elem = heap->elem; |
| 102 | + int elements = heap->elements; |
| 103 | + int left, right, max; |
| 104 | + |
| 105 | + while (true) { |
| 106 | + left = parent * 2; |
| 107 | + right = left + 1; |
| 108 | + |
| 109 | + max = parent; |
| 110 | + if (left <= elements && elem[max]->data < elem[left]->data) |
| 111 | + max = left; |
| 112 | + if (right <= elements && elem[max]->data < elem[right]->data) |
| 113 | + max = right; |
| 114 | + |
| 115 | + if (max == parent) |
| 116 | + break; |
| 117 | + |
| 118 | + swap(heap, max, parent); |
| 119 | + parent = max; |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +void build_heap(struct heap *heap) |
| 124 | +{ |
| 125 | + int i; |
| 126 | + |
| 127 | + for (i = heap->elements / 2; i >= 1; i--) |
| 128 | + heapify(heap, i); |
| 129 | +} |
| 130 | + |
| 131 | +int heap_sort(struct heap *heap) |
| 132 | +{ |
| 133 | + int elements = heap->elements; |
| 134 | + |
| 135 | + while (heap->elements) { |
| 136 | + swap(heap, 1, heap->elements); |
| 137 | + heap->elements--; |
| 138 | + heapify(heap, 1); |
| 139 | + } |
| 140 | + |
| 141 | + return elements; |
| 142 | +} |
| 143 | + |
| 144 | +int main() |
| 145 | +{ |
| 146 | + struct heap heap; |
| 147 | + int elements; |
| 148 | + |
| 149 | + fake_a_heap(&heap); |
| 150 | + dump_heap(&heap, 1); |
| 151 | + |
| 152 | + printf("After Heapify:\n"); |
| 153 | + build_heap(&heap); |
| 154 | + dump_heap(&heap, 1); |
| 155 | + |
| 156 | + printf("After Heap sort:\n"); |
| 157 | + elements = heap_sort(&heap); |
| 158 | + dump(&heap, elements); |
| 159 | + return 0; |
| 160 | +} |
0 commit comments