Updated April 1, 2023
Introduction to Bucket Sort in C
The bucket sort is a sorting algorithm to arrange elements in ascending order using a C programming language. It helps to partition array elements in a respective bucket as per element range using C language. The bucket sort is a sorting algorithm to arrange element range and keep it as per ascending or descending order. The bucket sort is a sorting algorithm to combine methods set up, scatter, sorting, and gather the array element. It is creating buckets as per element range and sorting array elements in ascending order using a C programming language. The bucket sort is a method to create an empty bucket and arrange elements as per the required order.
Syntax of Bucket Sort in C
You need an algorithm to know about bucket sort syntax.
The bucket sort algorithm is below:
- Create an empty bucket to place the array element.
- Insert array element in the bucket as per element range.
- Classify array elements as per required order.
- Write down the array element as per the sequence.
Bucket sort syntax is below:
void bucketSorting(int sort_array[], int array_length) {
int m, j;
int bucket[array_length];
for(m = 0; m < array_length; m++) {
bucket[m] = 0;
}
for(m = 0; m < array_length; m++) {
(bucket[sort_array[i]])++;
}
for(m = 0, j = 0; m < array_length; m++) {
for (; bucket[m] > 0; ( bucket [m])--) {
sort_array[j++] = m;
}
}
}
How does Bucket Sort Work in C?
Given below is the working:
1. Create an empty bucket to place the array element.
struct buckets
{
int number;
int* element;
};
2. Compare array elements in the bucket as per element range.
int comparevalues(const void* fvalue, const void* svalue)
{
int p = *((int*)fvalue), q = *((int*)svalue);
if (p == q)
{
return 0;
}
else if (p < q)
{
return -1;
}
else
{
return 1;
}
}
3. Classify array element as per required order.
void crtbucketsortingElement(int sortarry[], int arylength)
{
struct buckets crtbuckets[3];
int m, j, k;
for (m = 0; m < 3; m++)
{
crtbuckets[m].number = 0;
crtbuckets[m].element = (int*)malloc(sizeof(int) * arylength);
}
for (m = 0; m < arylength; m++)
{
if (sortarry[m] < 0)
{
crtbuckets[0].element[crtbuckets[0].number++] = sortarry[m];
}
else if (sortarry[m] > 10)
{
crtbuckets[2].element[crtbuckets[2].number++] = sortarry[m];
}
else
{
crtbuckets[1].element[crtbuckets[1].number++] = sortarry[m];
}
}
for (k = 0, m = 0; m < 3; m++)
{
qsort(crtbuckets[m].element, crtbuckets[m].number, sizeof(int), &comparevalues);
for (j = 0; j < crtbuckets[m].number; j++)
{
sortarry[k + j] = crtbuckets[m].element[j];
}
k = k+ crtbuckets[m].number;
free(crtbuckets[m].element);
}
}
4. Write down the array element as per the sequence.
int main(char *arg[]) {
int sortarry[100] = { 5, 9, 1, 2, 3, 2, 5, 4, 7, 0, 8, 6 };
int m = 12,j,k,arylength;
arylength = m;
printf("Before bucket Sorting array element: \n");
for (j = 0; j<m; j++)
{
printf("%d ", sortarry[j]);
}
crtbucketsortingElement(sortarry, arylength);
printf("\n After bucket Sorting array element: \n");
for (k = 0; k<m; k++)
printf("%d ", sortarry[k]);
return 0;
}
Examples of Bucket Sort in C
Given below are the examples mentioned:
Example #1
The bucket sort with positive value example and output are below.
Code:
#include <stdio.h>
#include <stdlib.h>
struct buckets
{
int number;
int* element;
};
int comparevalues(const void* fvalue, const void* svalue)
{
int p = *((int*)fvalue), q = *((int*)svalue);
if (p == q)
{
return 0;
}
else if (p < q)
{
return -1;
}
else
{
return 1;
}
}
void crtbucketsortingElement(int sortarry[], int arylength)
{
struct buckets crtbuckets[3];
int m, j, k;
for (m = 0; m < 3; m++)
{
crtbuckets[m].number = 0;
crtbuckets[m].element = (int*)malloc(sizeof(int) * arylength);
}
for (m = 0; m < arylength; m++)
{
if (sortarry[m] < 0)
{
crtbuckets[0].element[crtbuckets[0].number++] = sortarry[m];
}
else if (sortarry[m] > 10)
{
crtbuckets[2].element[crtbuckets[2].number++] = sortarry[m];
}
else
{
crtbuckets[1].element[crtbuckets[1].number++] = sortarry[m];
}
}
for (k = 0, m = 0; m < 3; m++)
{
qsort(crtbuckets[m].element, crtbuckets[m].number, sizeof(int), &comparevalues);
for (j = 0; j < crtbuckets[m].number; j++)
{
sortarry[k + j] = crtbuckets[m].element[j];
}
k = k+ crtbuckets[m].number;
free(crtbuckets[m].element);
}
}
int main(char *arg[]) {
int sortarry[100] = { 5, 91, 1, 112, 3, 2673, 54, 4000, 781, 100, 8, 61 };
int m = 12,j,k,arylength;
arylength = m;
printf("Before bucket Sorting array element: \n");
for (j = 0; j<m; j++)
{
printf("%d ", sortarry[j]);
}
crtbucketsortingElement(sortarry, arylength);
printf("\nAfter bucket Sorting array element: \n");
for (k = 0; k<m; k++)
printf("%d ", sortarry[k]);
return 0;
}
Output:
Example #2
The bucket sort with negative value example and output are below.
Code:
#include <stdio.h>
#include <stdlib.h>
struct buckets
{
int number;
int* element;
};
int comparevalues(const void* fvalue, const void* svalue)
{
int p = *((int*)fvalue), q = *((int*)svalue);
if (p == q)
{
return 0;
}
else if (p < q)
{
return -1;
}
else
{
return 1;
}
}
void crtbucketsortingElement(int sortarry[], int arylength)
{
struct buckets crtbuckets[3];
int m, j, k;
for (m = 0; m < 3; m++)
{
crtbuckets[m].number = 0;
crtbuckets[m].element = (int*)malloc(sizeof(int) * arylength);
}
for (m = 0; m < arylength; m++)
{
if (sortarry[m] < 0)
{
crtbuckets[0].element[crtbuckets[0].number++] = sortarry[m];
}
else if (sortarry[m] > 10)
{
crtbuckets[2].element[crtbuckets[2].number++] = sortarry[m];
}
else
{
crtbuckets[1].element[crtbuckets[1].number++] = sortarry[m];
}
}
for (k = 0, m = 0; m < 3; m++)
{
qsort(crtbuckets[m].element, crtbuckets[m].number, sizeof(int), &comparevalues);
for (j = 0; j < crtbuckets[m].number; j++)
{
sortarry[k + j] = crtbuckets[m].element[j];
}
k = k+ crtbuckets[m].number;
free(crtbuckets[m].element);
}
}
int main(char *arg[]) {
int sortarry[100] = { -5, -91, -1, -112, -3, -2673, -54, -4000, -781, -100, -8, -61 };
int m = 12,j,k,arylength;
arylength = m;
printf("Before bucket Sorting array element: \n");
for (j = 0; j<m; j++)
{
printf("%d ", sortarry[j]);
}
crtbucketsortingElement(sortarry, arylength);
printf("\nAfter bucket Sorting array element: \n");
for (k = 0; k<m; k++)
printf("%d ", sortarry[k]);
return 0;
}
Output:
Example #3
The bucket sort with combine values example and output are below.
Code:
#include <stdio.h>
#include <stdlib.h>
struct buckets
{
int number;
int* element;
};
int comparevalues(const void* fvalue, const void* svalue)
{
int p = *((int*)fvalue), q = *((int*)svalue);
if (p == q)
{
return 0;
}
else if (p < q)
{
return -1;
}
else
{
return 1;
}
}
void crtbucketsortingElement(int sortarry[], int arylength)
{
struct buckets crtbuckets[3];
int m, j, k;
for (m = 0; m < 3; m++)
{
crtbuckets[m].number = 0;
crtbuckets[m].element = (int*)malloc(sizeof(int) * arylength);
}
for (m = 0; m < arylength; m++)
{
if (sortarry[m] < 0)
{
crtbuckets[0].element[crtbuckets[0].number++] = sortarry[m];
}
else if (sortarry[m] > 10)
{
crtbuckets[2].element[crtbuckets[2].number++] = sortarry[m];
}
else
{
crtbuckets[1].element[crtbuckets[1].number++] = sortarry[m];
}
}
for (k = 0, m = 0; m < 3; m++)
{
qsort(crtbuckets[m].element, crtbuckets[m].number, sizeof(int), &comparevalues);
for (j = 0; j < crtbuckets[m].number; j++)
{
sortarry[k + j] = crtbuckets[m].element[j];
}
k = k+ crtbuckets[m].number;
free(crtbuckets[m].element);
}
}
int main(char *arg[]) {
int sortarry[100] = { -5, 91, -1, -112, 3, -2673, -54, 4000, -781, 100, 8, 61 };
int m = 12,j,k,arylength;
arylength = m;
printf("Before bucket Sorting array element: \n");
for (j = 0; j<m; j++)
{
printf("%d ", sortarry[j]);
}
crtbucketsortingElement(sortarry, arylength);
printf("\nAfter bucket Sorting array element: \n");
for (k = 0; k<m; k++)
printf("%d ", sortarry[k]);
return 0;
}
Output:
Conclusion
The bucket sort helps to arrange elements using an empty bucket. Its language helps to arrange array elements as per the user’s requirement. The bucket sort arranges data without the complexity and makes a user-friendly application.
Recommended Articles
This is a guide to Bucket Sort in C. Here we discuss the introduction, working and examples for a better understanding. You may also have a look at the following articles to learn more –