Fastest and simplest sort-bucket sort

Source: Internet
Author: User
Tags sort

We've learned a lot about sorting methods, bubble sort, hill sort, quick sort ... What I'm trying to say today is a sort of the fastest and easiest to apply--the bucket sort.

When you randomly enter a few numbers, order from small to large (from big to small), you will have a way.
This is just a one-dimensional array. Apply for a one-dimensional array of length n. is equivalent to the definition of n variables arr[0]~arr[n-1]; Initially initialized to 0, indicating that nothing is using it.

Down we add 1 to the value of the array of the following table for a number corresponding to the array. namely arr[0]=0,arr[1]=3,arr[5]=5......arr[n]=7; That corresponds to the number of occurrences of its subscript (a number appeared several times);

We call this method the bucket sort;
A bit like a hash table, but selects simple.

This algorithm, like having n buckets, numbered from 0~n-1, did not appear a number, just put an apple in the same place, the last number of apples in each bucket-the number of occurrences of each number.

Here is my Code implementation: for reference only

int main ()
{
    int arr[11]={0};
    int i=0;
    int j=0;
    int t;

    for (; i<11;i++)          //initializes the array to 0
        arr[i]=0;

    for (i=1;i<6;i++)
    {
        scanf ("%d", &t);   Loop input 5 number
        arr[t]++;      Save 5 entries in the corresponding array and record the number of
    } for

    (i=0;i<11;i++)
    {for
        (j=1;j<=arr[i];j++)  // Each element of the output array is
            printf ("%d", I);    The output of each array represents how much it is stored;
    }

    printf ("\ n");
    System ("pause");
    return 0;
}

You can extend .... hahaha

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.