changed the code a little bit

I changed the code a little bit and put in some comments.
This commit is contained in:
Christian Bender 2018-03-20 15:06:16 +01:00 committed by GitHub
parent 72e38936c2
commit 7efed28d14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,15 +4,16 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h>
#define NARRAY 8 /* array size */ #define NARRAY 8 /* array size */
#define NBUCKET 5 /* bucket size */ #define NBUCKET 5 /* bucket size */
#define INTERVAL 10 /* bucket range */ #define INTERVAL 10 /* bucket range */
struct Node struct Node
{ {
int data; int data;
struct Node *next; struct Node *next;
}; };
void BucketSort(int arr[]); void BucketSort(int arr[]);
@ -22,154 +23,178 @@ void printBuckets(struct Node *list);
int getBucketIndex(int value); int getBucketIndex(int value);
void BucketSort(int arr[]) void BucketSort(int arr[])
{ {
int i,j; int i,j;
struct Node **buckets; struct Node **buckets;
/* allocate memory for array of pointers to the buckets */ /* allocate memory for array of pointers to the buckets */
buckets = (struct Node **)malloc(sizeof(struct Node*) * NBUCKET); buckets = (struct Node **)malloc(sizeof(struct Node*) * NBUCKET);
/* initialize pointers to the buckets */ /* initialize pointers to the buckets */
for(i = 0; i < NBUCKET;++i) { for(i = 0; i < NBUCKET; ++i)
buckets[i] = NULL; {
} buckets[i] = NULL;
}
/* put items into the buckets */ /* put items into the buckets */
for(i = 0; i < NARRAY; ++i) { /* creates a link list in each bucket slot */
struct Node *current; for(i = 0; i < NARRAY; ++i)
int pos = getBucketIndex(arr[i]); {
current = (struct Node *) malloc(sizeof(struct Node)); struct Node *current;
current->data = arr[i]; int pos = getBucketIndex(arr[i]);
current->next = buckets[pos]; current = (struct Node *) malloc(sizeof(struct Node));
buckets[pos] = current; current->data = arr[i];
} current->next = buckets[pos];
buckets[pos] = current;
}
/* check what's in each bucket */ /* check what's in each bucket */
for(i = 0; i < NBUCKET; i++) { for(i = 0; i < NBUCKET; i++)
printf("Bucket[\"%d\"] : ", i); {
printBuckets(buckets[i]); printf("Bucket[\"%d\"] : ", i);
printf("\n"); printBuckets(buckets[i]);
} printf("\n");
}
/* sorting bucket using Insertion Sort */ /* sorting bucket using Insertion Sort */
for(i = 0; i < NBUCKET; ++i) { for(i = 0; i < NBUCKET; ++i)
buckets[i] = InsertionSort(buckets[i]); {
} buckets[i] = InsertionSort(buckets[i]);
}
/* check what's in each bucket */ /* check what's in each bucket */
printf("--------------\n"); printf("--------------\n");
printf("Buckets after sorted\n"); printf("Buckets after sorted\n");
for(i = 0; i < NBUCKET; i++) { for(i = 0; i < NBUCKET; i++)
printf("Bucket[\"%d\"] : ", i); {
printBuckets(buckets[i]); printf("Bucket[\"%d\"] : ", i);
printf("\n"); printBuckets(buckets[i]);
} printf("\n");
}
/* put items back to original array */ /* put items back to original array */
for(j =0, i = 0; i < NBUCKET; ++i) { for(j =0, i = 0; i < NBUCKET; ++i)
struct Node *node; {
node = buckets[i]; struct Node *node;
while(node) { node = buckets[i];
arr[j++] = node->data; while(node)
node = node->next; {
}
} // precondition for avoiding out of bounds by the array
assert(j < NARRAY);
/* free memory */ arr[j++] = node->data;
for(i = 0; i < NBUCKET;++i) { node = node->next;
struct Node *node; }
node = buckets[i]; }
while(node) {
struct Node *tmp; /* free memory */
tmp = node; for(i = 0; i < NBUCKET; ++i)
node = node->next; {
free(tmp); struct Node *node;
} node = buckets[i];
} while(node)
free(buckets); {
return; struct Node *tmp;
tmp = node;
node = node->next;
free(tmp);
}
}
free(buckets);
return;
} }
/* Insertion Sort */ /* Insertion Sort */
struct Node *InsertionSort(struct Node *list) struct Node *InsertionSort(struct Node *list)
{ {
struct Node *k,*nodeList; struct Node *k,*nodeList;
/* need at least two items to sort */ /* need at least two items to sort */
if(list == 0 || list->next == 0) { if(list == NULL || list->next == NULL)
return list; {
} 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;
}
for(ptr = nodeList; ptr->next != 0; ptr = ptr->next) { nodeList = list;
if(ptr->next->data > k->data) break; 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){ // from begin up to end
struct Node *tmp; // finds [i] > [i+1]
tmp = k; for(ptr = nodeList; ptr->next != NULL; ptr = ptr->next)
k = k->next; {
tmp->next = ptr->next; if(ptr->next->data > k->data) break;
ptr->next = tmp; }
continue;
} // if found (above)
else{ if(ptr->next != NULL)
ptr->next = k; {
k = k->next; struct Node *tmp;
ptr->next->next = 0; tmp = k;
continue; k = k->next; // important for the while
} tmp->next = ptr->next;
} ptr->next = tmp;
return nodeList; continue;
}
else
{
ptr->next = k;
k = k->next; // important for the while
ptr->next->next = NULL;
continue;
}
}
return nodeList;
} }
int getBucketIndex(int value) int getBucketIndex(int value)
{ {
return value/INTERVAL; return value/INTERVAL;
} }
void print(int ar[]) void print(int ar[])
{ {
int i; int i;
for(i = 0; i < NARRAY; ++i) { for(i = 0; i < NARRAY; ++i)
printf("%d ", ar[i]); {
} printf("%d ", ar[i]);
printf("\n"); }
printf("\n");
} }
void printBuckets(struct Node *list) void printBuckets(struct Node *list)
{ {
struct Node *cur = list; struct Node *cur = list;
while(cur) { while(cur)
printf("%d ", cur->data); {
cur = cur->next; printf("%d ", cur->data);
} cur = cur->next;
}
} }
int main(void) 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"); printf("Initial array\n");
print(array); print(array);
printf("------------\n"); printf("------------\n");
BucketSort(array); BucketSort(array);
printf("------------\n"); printf("------------\n");
printf("Sorted array\n"); printf("Sorted array\n");
print(array); print(array);
return 0; return 0;
} }