Bucket sequencing of common algorithms

Source: Internet
Author: User

Bucket sequencing should be the simplest sort method and because of the simplicity there are many limitations and drawbacks
The downside is that it's a waste of space. If you need to sort the numbers very large then apply for a large space

Here's one of the simplest examples to introduce bucket sequencing
If a teacher wants to score 5 students with a score of 0-10 points
The bucket sort method is to create an array of size 11, a[0]-a[10] is initialized to 0.
When there is a score of 1 then a[1] = 1; If there are two scores of 1, that a[1] = 2.
So according to this idea, we can simply write a program to verify

#include <stdio.h>

int main (void)
{
    int a[11],i,j,t;
    for (i = 0; I <=; i++)
    {
    A[i] = 0;
    }
    for (i = 1; I <= 5; i++)
    {
        scanf ("%d", &t);
        a[t]++;
    }
    for (i = 0; I <=; i++)
        for (j = 1; J <= A[i]; j + +)
        {
            printf ("%d", I);
        }
    return 0;
}

If the input is 5 1 3 8 2
Then the output is 12358.

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.