diff options
| -rw-r--r-- | heap.c | 73 |
1 files changed, 56 insertions, 17 deletions
| @@ -1,6 +1,7 @@ | |||
| 1 | #include <stdbool.h> | 1 | #include <stdbool.h> |
| 2 | #include <stdio.h> | 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> | 3 | #include <stdlib.h> |
| 4 | #include <time.h> | ||
| 4 | 5 | ||
| 5 | #include "util.h" | 6 | #include "util.h" |
| 6 | 7 | ||
| @@ -41,14 +42,17 @@ fixdown(T heap[], size_t i, size_t n) | |||
| 41 | const size_t r = RIGHT(i); | 42 | const size_t r = RIGHT(i); |
| 42 | size_t m = i; | 43 | size_t m = i; |
| 43 | 44 | ||
| 44 | if ( l < n && heap[m] < heap[l] ) | 45 | if ( l < n && heap[m] < heap[l] ) { |
| 45 | m = l; | 46 | m = l; |
| 47 | } | ||
| 46 | 48 | ||
| 47 | if ( r < n && heap[m] < heap[r] ) | 49 | if ( r < n && heap[m] < heap[r] ) { |
| 48 | m = r; | 50 | m = r; |
| 51 | } | ||
| 49 | 52 | ||
| 50 | if ( m == i ) | 53 | if ( m == i ) { |
| 51 | break; | 54 | break; |
| 55 | } | ||
| 52 | 56 | ||
| 53 | swap(heap, m, i); | 57 | swap(heap, m, i); |
| 54 | 58 | ||
| @@ -59,8 +63,9 @@ fixdown(T heap[], size_t i, size_t n) | |||
| 59 | static void | 63 | static void |
| 60 | heapify(T heap[], size_t n) | 64 | heapify(T heap[], size_t n) |
| 61 | { | 65 | { |
| 62 | for ( size_t i = n / 2 - 1; i >= 0; --i ) | 66 | for ( size_t i = n / 2; i-- > 0; ) { |
| 63 | fixdown(heap, i, n); | 67 | fixdown(heap, i, n); |
| 68 | } | ||
| 64 | } | 69 | } |
| 65 | 70 | ||
| 66 | static void | 71 | static void |
| @@ -68,7 +73,7 @@ my_heapsort(T a[], size_t n) | |||
| 68 | { | 73 | { |
| 69 | heapify(a, n); | 74 | heapify(a, n); |
| 70 | 75 | ||
| 71 | for ( size_t i = n - 1; i >= 0; --i ) { | 76 | for ( size_t i = n; i-- > 0; ) { |
| 72 | swap(a, 0, i); | 77 | swap(a, 0, i); |
| 73 | fixdown(a, 0, i); | 78 | fixdown(a, 0, i); |
| 74 | } | 79 | } |
| @@ -90,8 +95,9 @@ pq_init(struct pq *pq) | |||
| 90 | bool | 95 | bool |
| 91 | pq_push(struct pq *pq, T data) | 96 | pq_push(struct pq *pq, T data) |
| 92 | { | 97 | { |
| 93 | if ( pq->sz == NELEM(pq->heap) ) | 98 | if ( pq->sz == NELEM(pq->heap) ) { |
| 94 | return false; | 99 | return false; |
| 100 | } | ||
| 95 | 101 | ||
| 96 | pq->heap[pq->sz] = data; | 102 | pq->heap[pq->sz] = data; |
| 97 | fixup(pq->heap, pq->sz); | 103 | fixup(pq->heap, pq->sz); |
| @@ -102,8 +108,9 @@ pq_push(struct pq *pq, T data) | |||
| 102 | bool | 108 | bool |
| 103 | pq_pop(struct pq *pq, T *data) | 109 | pq_pop(struct pq *pq, T *data) |
| 104 | { | 110 | { |
| 105 | if ( pq->sz == 0 ) | 111 | if ( pq->sz == 0 ) { |
| 106 | return false; | 112 | return false; |
| 113 | } | ||
| 107 | 114 | ||
| 108 | *data = pq->heap[0]; | 115 | *data = pq->heap[0]; |
| 109 | --pq->sz; | 116 | --pq->sz; |
| @@ -119,26 +126,41 @@ print_heap(T heap[], size_t n) | |||
| 119 | { | 126 | { |
| 120 | if ( n ) { | 127 | if ( n ) { |
| 121 | printf("%d", heap[0]); | 128 | printf("%d", heap[0]); |
| 122 | for ( size_t i = 1; i != n; ++i ) | 129 | for ( size_t i = 1; i != n; ++i ) { |
| 123 | printf(", %d", heap[i]); | 130 | printf(", %d", heap[i]); |
| 131 | } | ||
| 124 | putchar('\n'); | 132 | putchar('\n'); |
| 125 | } | 133 | } |
| 126 | } | 134 | } |
| 127 | 135 | ||
| 128 | int | 136 | void |
| 129 | main(void) | 137 | test_heapsort(void) |
| 130 | { | 138 | { |
| 131 | #if 0 // Heap-Testprogramm | 139 | static const size_t N = 1001; |
| 132 | T heap[20] = { 18, 18, 16, 9, 7, 1, 9, 3, 7, 5 }; | 140 | T array[N]; |
| 133 | 141 | ||
| 134 | print_heap(heap, 10); | 142 | puts("fülle...."); |
| 143 | srand(time(NULL)); | ||
| 144 | for ( size_t idx = 0; idx != NELEM(array); ++idx ) { | ||
| 145 | array[idx] = rand(); | ||
| 146 | } | ||
| 135 | 147 | ||
| 136 | //heap[10] = 13; fixup(heap, 10); | 148 | puts("sortiere...."); |
| 137 | swap(heap, 0, 9); fixdown(heap, 0, 9); | 149 | my_heapsort(array, NELEM(array)); |
| 138 | 150 | ||
| 139 | print_heap(heap, 9); | 151 | puts("teste..."); |
| 140 | #endif | 152 | for ( size_t idx = 1; idx != NELEM(array); ++idx ) { |
| 153 | if ( array[idx - 1] > array[idx] ) { | ||
| 154 | fprintf(stderr, "Fehler an Pos: %zu\n", idx); | ||
| 155 | exit(EXIT_FAILURE); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | puts("ok"); | ||
| 159 | } | ||
| 141 | 160 | ||
| 161 | void | ||
| 162 | test_pq(void) | ||
| 163 | { | ||
| 142 | struct pq pq[1]; | 164 | struct pq pq[1]; |
| 143 | 165 | ||
| 144 | pq_init(pq); | 166 | pq_init(pq); |
| @@ -150,3 +172,20 @@ main(void) | |||
| 150 | while ( pq_pop(pq, &data) ) | 172 | while ( pq_pop(pq, &data) ) |
| 151 | printf("%d\n", data); | 173 | printf("%d\n", data); |
| 152 | } | 174 | } |
| 175 | |||
| 176 | int | ||
| 177 | main(void) | ||
| 178 | { | ||
| 179 | test_heapsort(); | ||
| 180 | test_pq(); | ||
| 181 | #if 0 // Heap-Testprogramm | ||
| 182 | T heap[20] = { 18, 18, 16, 9, 7, 1, 9, 3, 7, 5 }; | ||
| 183 | |||
| 184 | print_heap(heap, 10); | ||
| 185 | |||
| 186 | //heap[10] = 13; fixup(heap, 10); | ||
| 187 | swap(heap, 0, 9); fixdown(heap, 0, 9); | ||
| 188 | |||
| 189 | print_heap(heap, 9); | ||
| 190 | #endif | ||
| 191 | } | ||
