The bucket sort (Bucket sort) or the so-called box sort is a sort algorithm that works by splitting the array into a finite number of buckets. Each bucket is sorted individually (it is possible to use a different sort algorithm or to continue to sort by using a bucket sort recursively). Bucket sorting is a kind of inductive result of pigeon nest sorting. When the values in the array to be sorted are evenly distributed, the bucket sort uses a linear time (Θ (n)). But the bucket sort is not a comparison sort, and he is unaffected by the lower limit of O (n log n).
Bucket ordering is performed in the following procedure:
1. Set a quantitative array as empty bucket.
2. Search the sequence, and put a project one to the corresponding bucket.
3. Sort each bucket that is not empty.
4. Never empty bucket to put the item back into the original sequence.
Bucket sort Graphic sample
Bucket Sort Code:
* *
Bucket sort
*
parameter description:
* A--to be sorted array
* N--The length of the array a
* max--the range
of the maximum value in array A * * void Bucket_sort (int a[], int n, int max)
{
int i, J;
int *buckets;
if (a==null | | n<1 | | max<1) return
;
Creates an array buckets with a capacity of Max and initializes all data in buckets to 0.
if (buckets= (int *) malloc (max*sizeof (int)) ==null) return
;
memset (buckets, 0, max*sizeof (int));
1. Count for
(i = 0; i < n; i++)
buckets[a[i]]++;
2. Sort
for (i = 0, j = 0; i < max. i++) while
((buckets[i]--) >0)
a[j++] = i;
Free (buckets);
}
Description
Bucketsort (A, n, max) is the role of the array a bucket sort, n is the length of the arrays A, Max is the range of the largest element in the array [0,max).
Suppose a={8,2,3,4,3,6,6,3,9}, max=10. At this point, all the data for array A is placed in a bucket that needs to be 0-9. The following figure:
After putting the data into a bucket, the data in the bucket is presented and transformed into an ordered array by a certain algorithm. We'll get the results we want.