-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbucketSort.c
More file actions
45 lines (33 loc) · 781 Bytes
/
Copy pathbucketSort.c
File metadata and controls
45 lines (33 loc) · 781 Bytes
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
#include <stdio.h>
#define MAX 100
void bucketSort(int arr[], int n) {
int bucket[MAX] = {0};
for (int i = 0; i < n; i++)
bucket[arr[i]]++;
int idx = 0;
for (int i = 0; i < MAX; i++) {
while (bucket[i]-- > 0)
arr[idx++] = i;
}
}
void printArray(int arr[], int n) {
printf("Array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[100], n;
printf("Enter number of elements: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Element %d (0–99): ", i + 1);
scanf("%d", &arr[i]);
}
printf("Original ");
printArray(arr, n);
bucketSort(arr, n);
printf("Sorted ");
printArray(arr, n);
return 0;
}