桶分类 算法
桶分类 (Bucket Sort)
Bucket sort is a sorting technique in which array is partitioned into the buckets. By this, each bucket will be sorted individually, by using some another sorting algorithm such as insertion sort. This sorting technique assumes that the input is drawn from a uniform distribution by which it has an average case of O(n). Bucket sort is a fast technique because bucket sort presumes that the input is generated by a process, which will distribute the element uniformly and independently over the interval.
存储桶排序是一种将数组划分为存储桶的排序技术。 这样,将使用其他一些排序算法(例如插入排序)分别对每个存储桶进行排序。 这种排序技术假定输入是从均匀分布中提取的,该输入的平均情况为O(n) 。 桶分类是一种快速的技术,因为桶分类假定输入是由某个进程生成的,该输入将在间隔内均匀且独立地分配元素。
桶分类算法 (Algorithm for Bucket Sort)
Set up an array of initially empty buckets.
设置一系列最初为空的存储桶。
Put the element in the corresponding bucket.
将元素放入相应的存储桶中。
Sort each non-empty bucket.
对每个非空桶进行排序。
Visit the bucket in order and put all the elements into a sequence and print them.
按顺序访问存储桶,并将所有元素放入序列中并打印。
桶分类的伪代码 (Pseudo code for Bucket Sort)
void bucketsort (int a[ ], int n, int max)
{
int i,j=0;
//initialize each bucket 0 and then make bucket empty.
int* buckets = calloc(max+1, size of (int));
for(int i=0; i<n; i++)
buckets[a[i]]++;
//now sort each bucket individually.
//sequentially empty each bucket in some array.
for(i=0; i<max; i++)
while (buckets[i]--)
b[j++]=i;
//display the array b as sorted list of elements.
}
Example:
例:
Let us sort the elements by using bucket sort. Elements which need to be sort are 56, 12, 84, 28, 0,-13, 47, 94, 31, 12.
让我们使用存储桶排序对元素进行排序。 需要排序的元素是56,12,84,28,0,-13,47,94,31,12。
Step 1) First set up an array which is given below:
步骤1)首先建立一个数组,如下所示:
Step 2) Now consider the range such that:
步骤2)现在考虑范围,使得:
-20 to -1, 0 to 10 10 to 20, 20 to 30, 30 to 40, 40 to 50, 50 to 60, 60 to 70, 70 to 80, 80 to 90, 90 to 100.
-20至-1、0至10 10至20、20至30、30至40、40至50、50至60、60至70、70至80、80至90、90至100。
Now we fill up each bucket by given elements,
现在,我们通过给定的元素填充每个存储桶,
Step 3) Now sort the each bucket and then print the array by visiting each bucket sequentially.
步骤3)现在,对每个存储桶进行排序,然后依次访问每个存储桶以打印阵列。
-13, 0, 12, 12, 28, 31, 47, 56, 84, 94
-13、0、12、12、28、31、47、56、84、94
This is the sorted list.
这是排序的列表。
桶分类的缺点 (Drawbacks of Bucket Sort)
For the bucket sort, it’s the necessary part that the maximum value of the element must be known.
对于存储桶排序,必须知道元素的最大值是必不可少的部分。
In this type of technique, we have to create enough buckets in the memory for every element, to place in the array.
在这种技术中,我们必须在内存中为每个元素创建足够的存储桶,以放置在数组中。
桶排序分析 (Analysis of the bucket Sort)
Average case, best case, and worst case time complexity of this algorithm is O(n).
该算法的平均情况,最佳情况和最坏情况的时间复杂度为O(n) 。
翻译自: https://www.includehelp.com/algorithms/bucket-sort-algorithm.aspx
桶分类 算法