-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathbucket_sort.c
51 lines (51 loc) · 1.08 KB
/
bucket_sort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Bucket Sort in C programming
#include <stdio.h>
int getMax(int array[], int size)
{
int max = array[0];
for (int i = 1; i < size; i++)
if (array[i] > max)
max = array[i];
return max;
}
void bucketSort(int array[], int size)
{
// The size of bucket must be at least the (max+1) but
// we cannot assign declare it as int bucket(max+1) in C as
// it does not support dynamic memory allocation.
// So, its size is provided statically.
int bucket[10];
const int max = getMax(array, size);
for (int i = 0; i <= max; i++)
{
bucket[i] = 0;
}
for (int i = 0; i < size; i++)
{
bucket[array[i]]++;
}
for (int i = 0, j = 0; i <= max; i++)
{
while (bucket[i] > 0)
{
array[j++] = i;
bucket[i]--;
}
}
}
void printArray(int array[], int size)
{
for (int i = 0; i < size; ++i)
{
printf("%d ", array[i]);
}
printf("\n");
}
int main()
{
int data[] = {4, 3, 4, 5, 6, 0, 9, 5};
int size = sizeof(data) / sizeof(data[0]);
bucketSort(data, size);
printf("Sorted array in ascending order: \n");
printArray(data, size);
}