-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucketsort.c
56 lines (50 loc) · 1.3 KB
/
bucketsort.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
52
53
54
55
56
#include <stdio.h>
void Bucket_Sort(int array[], int n, int max, int min)
{
int i, j = 0;
int cpos[max+1], cneg[-(min-1)];
for (i = 0; i <= max; i++)
cpos[i] = 0;
for (i = 0; i <= -(min-1); i++)
cneg[i] = 0;
for (i = 0; i < n; i++)
{
if (array[i] >= 0)
(cpos[array[i]])++;
else
(cneg[-array[i]])++;
}
for (i = -min; i > 0; i--)
for (; cneg[i] > 0; (cneg[i])--)
array[j++] = -i;
for (i = 0; i <= max; i++)
for (; cpos[i] > 0; (cpos[i])--)
array[j++] = i;
}
int main()
{
int array[100], i, num, max = 0, min = 0;
printf("Enter the size of array : ");
scanf("%d", &num);
printf("Enter the %d elements to be sorted:\n",num);
for (i = 0; i < num; i++)
scanf("%d", &array[i]);
for (i = 0; i < num; i++)
{
if (array[i] > max)
max = array[i];
if (array[i] < min)
min = array[i];
}
if (min >= 0)min = 0;
printf("\nThe array of elements before sorting : \n");
for (i = 0; i < num; i++)
printf("%d ", array[i]);
printf("\nThe array of elements after sorting : \n");
Bucket_Sort(array, num, max, min);
for (i = 0; i < num; i++)
{
printf("%d ", array[i]);
}
return 0;
}