📅  最后修改于: 2020-10-15 01:22:16             🧑  作者: Mango
桶分类也称为垃圾桶分类。它通过将元素分布到也称为存储桶的数组中来工作。使用不同的排序算法分别对存储桶进行排序。
Algorithm | Complexity |
---|---|
Space | O(1) |
Worst Case | O(n2) |
Best Case | Ω(n+k) |
Average Case | θ(n+k) |
程序
#include
void Bucket_Sort(int array[], int n)
{
int i, j;
int count[n];
for (i = 0; i < n; i++)
count[i] = 0;
for (i = 0; i < n; i++)
(count[array[i]])++;
for (i = 0, j = 0; i < n; i++)
for(; count[i] > 0; (count[i])--)
array[j++] = i;
}
/* End of Bucket_Sort() */
/* The main() begins */
int main()
{
int array[100], i, num;
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]);
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);
for (i = 0; i < num; i++)
printf("%d ", array[i]);
printf("\n");
return 0;
}