Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added bucket_sort.c and Modified Readme.md #1422

Merged
merged 1 commit into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions Sorting/Bucket Sort/C/bucket_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*Bucket Sort is a sorting technique that sorts the elements by first dividing the elements into several groups called buckets.
The elements inside each bucket are sorted using any of the suitable sorting algorithms or recursively calling the same algorithm.

Several buckets are created. Each bucket is filled with a specific range of elements.
The elements inside the bucket are sorted using any other algorithm.
Finally, the elements of the bucket are gathered to get the sorted array.*/

#include <stdio.h>
#include <stdlib.h>

#define ARRAY 10 // Array size
#define BUCKET 9 // Number of buckets
#define BREAK 13 // Each bucket capacity

struct Node {
int data;
struct Node *next;
};

void bucketsort(int arr[]);
struct Node *InsertionSort(struct Node *list);
void print(int arr[]);
void printBuckets(struct Node *list);
int getBucketIndex(int value);

// Sorting function
void bucketsort(int arr[]) {
int i, j;
struct Node **buckets;

// Create buckets and allocate memory size
buckets = (struct Node **)malloc(sizeof(struct Node *) * BUCKET);

// Initialize empty buckets
for (i = 0; i < BUCKET; ++i) {
buckets[i] = NULL;
}

// Fill the buckets with respective elements
for (i = 0; i < ARRAY; ++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;
}

// Print the buckets along with their elements
for (i = 0; i < BUCKET; i++) {
printf("Bucket[%d]: ", i);
printBuckets(buckets[i]);
printf("\n");
}

// Sort the elements of each bucket
for (i = 0; i < BUCKET; ++i) {
buckets[i] = InsertionSort(buckets[i]);
}

printf("-------------\n");
printf("Bucktets after sorting\n");
for (i = 0; i < BUCKET; i++) {
printf("Bucket[%d]: ", i);
printBuckets(buckets[i]);
printf("\n");
}

// Put sorted elements on arr
for (j = 0, i = 0; i < BUCKET; ++i) {
struct Node *node;
node = buckets[i];
while (node) {
arr[j++] = node->data;
node = node->next;
}
}

return;
}

// Function to sort the elements of each bucket
struct Node *InsertionSort(struct Node *list) {
struct Node *k, *nodeList;
if (list == 0 || list->next == 0) {
return list;
}

nodeList = list;
k = list->next;
nodeList->next = 0;
while (k != 0) {
struct Node *ptr;
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) {
if (ptr->next->data > k->data)
break;
}

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;
}

int getBucketIndex(int value) {
return value / BREAK;
}

void print(int ar[]) {
int i;
for (i = 0; i < ARRAY; ++i) {
printf("%d ", ar[i]);
}
printf("\n");
}

// Print buckets
void printBuckets(struct Node *list) {
struct Node *cur = list;
while (cur) {
printf("%d ", cur->data);
cur = cur->next;
}
}

// Driver code
int main(void) {
int array[ARRAY] = {44, 23, 33, 62, 57, 47, 51, 88, 14, 66};

printf("Initial array: ");
print(array);
printf("\n");

bucketsort(array);
printf("\n");
printf("Sorted array: ");
print(array);
return 0;
}
2 changes: 1 addition & 1 deletion Sorting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
|:--------------|:----------------:|:----------------:|:----------------:|:-----------------:|:-----------------:|:-----------------:|:-----------------:|:-----------------:|:-----------------:|:-----------------:|
| [Bogo Sort](https://en.wikipedia.org/wiki/Bogosort) | [:heavy_check_mark:](Bogosort/C/bogosort.c) | [:heavy_check_mark:](Bogosort/C%2B%2B/Bogosort.cpp) | [:heavy_check_mark:](Bogosort/Java/Bogosort.java) | [:heavy_check_mark:](Bogosort/Python/bogosort.py) | | [:heavy_check_mark:](Bogosort/C%20Sharp/Bogosort.cs) | | [:heavy_check_mark:](Bogosort/Ruby/bogosort.rb) | | |
| [Bubble Sort](https://en.wikipedia.org/wiki/Bubble_sort) | [:heavy_check_mark:](Bubble%20Sort/C/bubble_sort.c) | [:heavy_check_mark:](Bubble%20Sort/CPP/bubble_sort.cpp) | [:heavy_check_mark:](Bubble%20Sort/Java/BubbleSort.java) | [:heavy_check_mark:](Bubble%20Sort/Python/bubble_sort.py) | [:heavy_check_mark:](Bubble%20Sort/Javascript/bubble_sort.js) | [:heavy_check_mark:](Bubble%20Sort/C%20Sharp/BubbleSort.cs) | [:heavy_check_mark:](Bubble%20Sort/Go/bubble_sort.go) | [:heavy_check_mark:](Bubble%20Sort/Ruby/bubble_sort.rb) | [:heavy_check_mark:](Bubble%20Sort/Rust/bubble_sort.rs) | [:heavy_check_mark:](Bubble%20Sort/Swift/bubble_sort.swift) |
|[Bucket Sort](https://en.wikipedia.org/wiki/Bucket_sort) | | [:heavy_check_mark:](Bucket%20Sort/C%2B%2B/bucket_sort.cpp) | [:heavy_check_mark:](Bucket%20Sort/Java/bucket_sort.java) | | [:heavy_check_mark:](Bucket%20Sort/Javascript/bucket_sort.js) | | | | | |
|[Bucket Sort](https://en.wikipedia.org/wiki/Bucket_sort) |[:heavy_check_mark:](Bucket%20Sort/C/bucket_sort.c) | [:heavy_check_mark:](Bucket%20Sort/C%2B%2B/bucket_sort.cpp) | [:heavy_check_mark:](Bucket%20Sort/Java/bucket_sort.java) | | [:heavy_check_mark:](Bucket%20Sort/Javascript/bucket_sort.js) | | | | | |
| [Counting Sort](https://en.wikipedia.org/wiki/Counting_sort) | [:heavy_check_mark:](Counting%20Sort/C/Counting_sort.c) | [:heavy_check_mark:](Counting%20Sort/CPP/counting_sort.cpp) | [:heavy_check_mark:](Counting%20Sort/Java/counting_sort.java) | [:heavy_check_mark:](Counting%20Sort/Python/counting_sort.py) | [:heavy_check_mark:](Counting%20Sort/Javascript/counting_sort.js) | | [:heavy_check_mark:](Counting%20Sort/Golang/count-sort.go) | [:heavy_check_mark:](Counting%20Sort/Ruby/countSort.rb) | [:heavy_check_mark:](Counting%20Sort/Rust/counting-sort.rs)| |
| [Heap Sort](https://en.wikipedia.org/wiki/Heapsort) | [:heavy_check_mark:](Heapsort/C/Heapsort.c) | [:heavy_check_mark:](Heapsort/C%2B%2B/heapsort.cpp) | [:heavy_check_mark:](Heapsort/Java/HeapSort.java) | [:heavy_check_mark:](Heapsort/Python/heapsort.py) | [:heavy_check_mark:](Heapsort/Javascript/heapsort.js) | [:heavy_check_mark:](Heapsort/C%20Sharp/heapsort.cs) | | [:heavy_check_mark:](Heapsort/Ruby/heapsort.rb) | | |
| [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort) | [:heavy_check_mark:](Insertion%20Sort/C/insertion_sort.c) | [:heavy_check_mark:](Insertion%20Sort/C%2B%2B/insertion_sort.cpp) | [:heavy_check_mark:](Insertion%20Sort/Java/insertion_sort.java) | [:heavy_check_mark:](Insertion%20Sort/Python/insertion_sort.py) | [:heavy_check_mark:](Insertion%20Sort/Javascript/insertion_sort.js) | | [:heavy_check_mark:](Insertion%20Sort/Go/insertion_sort.go) | [:heavy_check_mark:](Insertion%20Sort/Ruby/insertion_sort.rb) | [:heavy_check_mark:](Insertion%20Sort/Rust/insertion_sort.rs) | |
Expand Down