The understanding of bucket sorting algorithm and the C language version code example _c language

Source: Internet
Author: User

Understand:
Bucket Sort is a variant of the counting sort, placing the adjacent M "bucket" in the counting order in a "big bucket", sorting each bucket after the barrel is finished (usually with a fast row), then merging into the final result.
Basic idea:
The bucket sort assumption sequence is generated by a random process that distributes the elements evenly and independently on the interval [0,1]. We divide the interval [0,1] into N a subgroup of the same size, called a bucket. Distribute n records to each bucket. If more than one record is divided into the same bucket, the order of the buckets is required. Finally, the records in each bucket are listed in order to remember the ordered sequence.
Efficiency Analysis:
The average time complexity of the bucket ordering is linear O (n+c), where C is the time complexity of the barrel's fast row. If the bucket number m is larger than the same N, its efficiency is higher and the best time complexity reaches O (n). Of course the space complexity of the bucket sort is O (n+m), if the input data is very large, and the number of barrels is very large, the space cost is undoubtedly expensive. In addition, the bucket sort is stable.
The disadvantage of the bucket order is that if only a few numbers, but the range of the number is very large (10 number, the range of the number of 0~10000000), then we need 10,000,001 barrels to be able, even 10 number.

Example
Question 1:
randomly enter 5 numbers, from large to small output.
Idea: By using an array of ranges based on the maximum and minimum number of input digits, each time a number is entered, the number is inserted into the ordinal of the corresponding array.

#include <stdio.h>
int main ()
{
 int a[11],i,j,t;
 Initialize bucket array for
 (i=0;i<=10;i++)
 {
   A[i] = 0;
 }
 Loop reads 5 number
 for (i = 1;i<=5;i++)
 {
   //read each number to the variable
   scanf ("%d", &t);
   Count  
   a[t]++;
 }
 From large to small output
 for (i = 10;i>=0;i--)
 {
   for (j=1;j<=a[i];j++)
     printf ("%d", I);
 }
 GetChar (); GetChar ()
 ; GetChar () is used to pause the program to view the contents of the program output
 //or to use System ("pause") instead of return
 0;
}

Question 2: sort 0-1000 of integers

#include <stdio.h>
int main ()
{
 int book[1001],i,j,t;
 Initialize bucket array for
 (i=0;i<=1000;i++)
 {
   Book[i] = 0;
 }
 Enter a number n to indicate the next n number
 scanf ("%d", &n);
 for (i = 1;i<=n;i++)
 {
   //read each number to the variable
   scanf ("%d", &t);
   Count  
   book[t]++;
 }
 From large to small output
 for (i = 1000;i>=0;i--)
 {
   for (j=1;j<=book[i];j++)
     printf ("%d", I);
 }
 GetChar (); GetChar ()
 ; return 0;
}

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: info-contact@alibabacloud.com 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.