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,6 +4,7 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define NARRAY 8 /* array size */
#define NBUCKET 5 /* bucket size */
@ -30,12 +31,15 @@ void BucketSort(int arr[])
buckets = (struct Node **)malloc(sizeof(struct Node*) * NBUCKET);
/* initialize pointers to the buckets */
for(i = 0; i < NBUCKET;++i) {
for(i = 0; i < NBUCKET; ++i)
{
buckets[i] = NULL;
}
/* put items into the buckets */
for(i = 0; i < NARRAY; ++i) {
/* 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));
@ -45,41 +49,51 @@ void BucketSort(int arr[])
}
/* 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("\n");
}
/* sorting bucket using Insertion Sort */
for(i = 0; i < NBUCKET; ++i) {
for(i = 0; i < NBUCKET; ++i)
{
buckets[i] = InsertionSort(buckets[i]);
}
/* check what's in each bucket */
printf("--------------\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("\n");
}
/* 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];
while(node) {
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) {
for(i = 0; i < NBUCKET; ++i)
{
struct Node *node;
node = buckets[i];
while(node) {
while(node)
{
struct Node *tmp;
tmp = node;
node = node->next;
@ -95,41 +109,50 @@ struct Node *InsertionSort(struct Node *list)
{
struct Node *k,*nodeList;
/* need at least two items to sort */
if(list == 0 || list->next == 0) {
if(list == NULL || list->next == NULL)
{
return list;
}
nodeList = list;
k = list->next;
nodeList->next = 0; /* 1st node is new list */
while(k != 0) {
nodeList->next = NULL; /* 1st node is new list */
while(k != NULL)
{
struct Node *ptr;
/* check if insert before first */
if(nodeList->data > k->data) {
if(nodeList->data > k->data)
{
struct Node *tmp;
tmp = k;
k = k->next;
k = k->next; // important for the while
tmp->next = nodeList;
nodeList = tmp;
continue;
}
for(ptr = nodeList; ptr->next != 0; ptr = ptr->next) {
// 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(ptr->next!=0){
// if found (above)
if(ptr->next != NULL)
{
struct Node *tmp;
tmp = k;
k = k->next;
k = k->next; // important for the while
tmp->next = ptr->next;
ptr->next = tmp;
continue;
}
else{
else
{
ptr->next = k;
k = k->next;
ptr->next->next = 0;
k = k->next; // important for the while
ptr->next->next = NULL;
continue;
}
}
@ -144,7 +167,8 @@ int getBucketIndex(int value)
void print(int ar[])
{
int i;
for(i = 0; i < NARRAY; ++i) {
for(i = 0; i < NARRAY; ++i)
{
printf("%d ", ar[i]);
}
printf("\n");
@ -153,7 +177,8 @@ void print(int ar[])
void printBuckets(struct Node *list)
{
struct Node *cur = list;
while(cur) {
while(cur)
{
printf("%d ", cur->data);
cur = cur->next;
}