From 7efed28d14d92d9105adb878de8c6186c940611c Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Tue, 20 Mar 2018 15:06:16 +0100 Subject: [PATCH] changed the code a little bit I changed the code a little bit and put in some comments. --- sorting/BucketSort.c | 265 +++++++++++++++++++++++-------------------- 1 file changed, 145 insertions(+), 120 deletions(-) diff --git a/sorting/BucketSort.c b/sorting/BucketSort.c index 29009567..12b5eb7e 100644 --- a/sorting/BucketSort.c +++ b/sorting/BucketSort.c @@ -4,15 +4,16 @@ */ #include #include +#include #define NARRAY 8 /* array size */ #define NBUCKET 5 /* bucket size */ #define INTERVAL 10 /* bucket range */ -struct Node -{ - int data; - struct Node *next; +struct Node +{ + int data; + struct Node *next; }; void BucketSort(int arr[]); @@ -22,154 +23,178 @@ void printBuckets(struct Node *list); int getBucketIndex(int value); void BucketSort(int arr[]) -{ - int i,j; - struct Node **buckets; +{ + int i,j; + struct Node **buckets; - /* allocate memory for array of pointers to the buckets */ - buckets = (struct Node **)malloc(sizeof(struct Node*) * NBUCKET); + /* allocate memory for array of pointers to the buckets */ + buckets = (struct Node **)malloc(sizeof(struct Node*) * NBUCKET); - /* initialize pointers to the buckets */ - for(i = 0; i < NBUCKET;++i) { - buckets[i] = NULL; - } + /* initialize pointers to the buckets */ + for(i = 0; i < NBUCKET; ++i) + { + buckets[i] = NULL; + } - /* put items into the buckets */ - for(i = 0; i < NARRAY; ++i) { - struct Node *current; - int pos = getBucketIndex(arr[i]); - current = (struct Node *) malloc(sizeof(struct Node)); - current->data = arr[i]; - current->next = buckets[pos]; - buckets[pos] = current; - } + /* put items into the buckets */ + /* creates a link list in each bucket slot */ + for(i = 0; i < NARRAY; ++i) + { + struct Node *current; + int pos = getBucketIndex(arr[i]); + current = (struct Node *) malloc(sizeof(struct Node)); + current->data = arr[i]; + current->next = buckets[pos]; + buckets[pos] = current; + } - /* check what's in each bucket */ - for(i = 0; i < NBUCKET; i++) { - printf("Bucket[\"%d\"] : ", i); - printBuckets(buckets[i]); - printf("\n"); - } + /* check what's in each bucket */ + for(i = 0; i < NBUCKET; i++) + { + printf("Bucket[\"%d\"] : ", i); + printBuckets(buckets[i]); + printf("\n"); + } - /* sorting bucket using Insertion Sort */ - for(i = 0; i < NBUCKET; ++i) { - buckets[i] = InsertionSort(buckets[i]); - } + /* sorting bucket using Insertion Sort */ + for(i = 0; i < NBUCKET; ++i) + { + buckets[i] = InsertionSort(buckets[i]); + } - /* check what's in each bucket */ + /* check what's in each bucket */ printf("--------------\n"); printf("Buckets after sorted\n"); - for(i = 0; i < NBUCKET; i++) { - printf("Bucket[\"%d\"] : ", i); - printBuckets(buckets[i]); - printf("\n"); - } + for(i = 0; i < NBUCKET; i++) + { + printf("Bucket[\"%d\"] : ", i); + printBuckets(buckets[i]); + printf("\n"); + } - /* put items back to original array */ - for(j =0, i = 0; i < NBUCKET; ++i) { - struct Node *node; - node = buckets[i]; - while(node) { - arr[j++] = node->data; - node = node->next; - } - } - - /* free memory */ - for(i = 0; i < NBUCKET;++i) { - struct Node *node; - node = buckets[i]; - while(node) { - struct Node *tmp; - tmp = node; - node = node->next; - free(tmp); - } - } - free(buckets); - return; + /* put items back to original array */ + for(j =0, i = 0; i < NBUCKET; ++i) + { + struct Node *node; + node = buckets[i]; + while(node) + { + + // precondition for avoiding out of bounds by the array + assert(j < NARRAY); + arr[j++] = node->data; + node = node->next; + } + } + + /* free memory */ + for(i = 0; i < NBUCKET; ++i) + { + struct Node *node; + node = buckets[i]; + while(node) + { + struct Node *tmp; + tmp = node; + node = node->next; + free(tmp); + } + } + free(buckets); + return; } /* Insertion Sort */ struct Node *InsertionSort(struct Node *list) -{ - struct Node *k,*nodeList; - /* need at least two items to sort */ - if(list == 0 || list->next == 0) { - return list; - } - - nodeList = list; - k = list->next; - nodeList->next = 0; /* 1st node is new list */ - while(k != 0) { - struct Node *ptr; - /* check if insert before first */ - if(nodeList->data > k->data) { - struct Node *tmp; - tmp = k; - k = k->next; - tmp->next = nodeList; - nodeList = tmp; - continue; - } +{ + struct Node *k,*nodeList; + /* need at least two items to sort */ + if(list == NULL || list->next == NULL) + { + return list; + } - for(ptr = nodeList; ptr->next != 0; ptr = ptr->next) { - if(ptr->next->data > k->data) break; - } + nodeList = list; + k = list->next; + nodeList->next = NULL; /* 1st node is new list */ + while(k != NULL) + { + struct Node *ptr; + /* check if insert before first */ + if(nodeList->data > k->data) + { + struct Node *tmp; + tmp = k; + k = k->next; // important for the while + tmp->next = nodeList; + nodeList = tmp; + continue; + } - if(ptr->next!=0){ - struct Node *tmp; - tmp = k; - k = k->next; - tmp->next = ptr->next; - ptr->next = tmp; - continue; - } - else{ - ptr->next = k; - k = k->next; - ptr->next->next = 0; - continue; - } - } - return nodeList; + // from begin up to end + // finds [i] > [i+1] + for(ptr = nodeList; ptr->next != NULL; ptr = ptr->next) + { + if(ptr->next->data > k->data) break; + } + + // if found (above) + if(ptr->next != NULL) + { + struct Node *tmp; + tmp = k; + k = k->next; // important for the while + tmp->next = ptr->next; + ptr->next = tmp; + continue; + } + else + { + ptr->next = k; + k = k->next; // important for the while + ptr->next->next = NULL; + continue; + } + } + return nodeList; } int getBucketIndex(int value) { - return value/INTERVAL; + return value/INTERVAL; } void print(int ar[]) -{ - int i; - for(i = 0; i < NARRAY; ++i) { - printf("%d ", ar[i]); - } - printf("\n"); +{ + int i; + for(i = 0; i < NARRAY; ++i) + { + printf("%d ", ar[i]); + } + printf("\n"); } void printBuckets(struct Node *list) { - struct Node *cur = list; - while(cur) { - printf("%d ", cur->data); - cur = cur->next; - } + struct Node *cur = list; + while(cur) + { + printf("%d ", cur->data); + cur = cur->next; + } } int main(void) -{ - int array[NARRAY] = {29,25,-1,49,9,37,21,43}; +{ + int array[NARRAY] = {29,25,-1,49,9,37,21,43}; printf("Initial array\n"); print(array); printf("------------\n"); - BucketSort(array); + BucketSort(array); printf("------------\n"); - printf("Sorted array\n"); - print(array); - return 0; -} \ No newline at end of file + printf("Sorted array\n"); + print(array); + return 0; +}