From f679f8cf7791e00d1d28fb71fa95b3c2cb17068c Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 29 Nov 2020 08:28:16 +0100 Subject: erste Version mit size_t statt int als Index-Typ. --- heap.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/heap.c b/heap.c index 7976e52..b9c15fa 100644 --- a/heap.c +++ b/heap.c @@ -13,7 +13,7 @@ typedef int T; #define PARENT(idx) ((idx - 1) / 2) static void -swap(T heap[], int i, int j) +swap(T heap[], size_t i, size_t j) { T temp = heap[i]; heap[i] = heap[j]; @@ -21,9 +21,9 @@ swap(T heap[], int i, int j) } static void -fixup(T heap[], int i) +fixup(T heap[], size_t i) { - int p = PARENT(i); + size_t p = PARENT(i); while ( i > 0 && heap[p] < heap[i] ) { swap(heap, i, p); @@ -34,12 +34,12 @@ fixup(T heap[], int i) } static void -fixdown(T heap[], int i, int n) +fixdown(T heap[], size_t i, size_t n) { for ( ;; ) { - const int l = LEFT(i); - const int r = RIGHT(i); - int m = i; + const size_t l = LEFT(i); + const size_t r = RIGHT(i); + size_t m = i; if ( l < n && heap[m] < heap[l] ) m = l; @@ -57,18 +57,18 @@ fixdown(T heap[], int i, int n) } static void -heapify(T heap[], int n) +heapify(T heap[], size_t n) { - for ( int i = n / 2 - 1; i >= 0; --i ) + for ( size_t i = n / 2 - 1; i >= 0; --i ) fixdown(heap, i, n); } static void -my_heapsort(T a[], int n) +my_heapsort(T a[], size_t n) { heapify(a, n); - for ( int i = n - 1; i >= 0; --i ) { + for ( size_t i = n - 1; i >= 0; --i ) { swap(a, 0, i); fixdown(a, 0, i); } @@ -77,8 +77,8 @@ my_heapsort(T a[], int n) // ------------------------------------------- struct pq { // Priority Queue - T heap[251]; - int sz; + T heap[251]; + size_t sz; }; void @@ -115,11 +115,11 @@ pq_pop(struct pq *pq, T *data) // ------------------------------------------- void -print_heap(T heap[], int n) +print_heap(T heap[], size_t n) { if ( n ) { printf("%d", heap[0]); - for ( int i = 1; i != n; ++i ) + for ( size_t i = 1; i != n; ++i ) printf(", %d", heap[i]); putchar('\n'); } -- cgit v1.3