Detailed bucket sort bucket sorting algorithm and C + + code implementation Example _c language

Source: Internet
Author: User

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++) 

  2. Sort
  for (i = 0, j = 0; i < max. i++) while 
    ((buckets[i]--) >0)
      a[j++] = i;

  Free (buckets);

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.