Bucket sort [fastest and simplest sort]

Source: Internet
Author: User

5 numbers to sort, 5,3,5,2,8

First we need to apply for an array of size 11 int a[11]. Now you have 11 variables, numbered from a[0]~a[10]. At first, we initialized the a[0]~a[10] to 0, indicating that none of the scores were ever.


Next start to deal with each person's score, the first person's score is 5 points, we will correspond to the value of a[5] in the original base increased 1, the value of a[5] from 0 to 1, indicating that 5 points appeared once. The final result in turn

#include <stdio.h> int main () {     int a[11],i,j,t;     for (i=0;i<=10;i++)        a[i]=0;//initialized to 0     for (i=1;i<=5;i++)//loop read in 5 numbers    {          scanf ("%d", &t); Read each of the numbers into the variable t       a[t]++;//Count    } for     (i=0;i<=10;i++)///[a[0]~a[10] for        (j=1;j<=a[i];j++)// Several times appeared on the print several times          printf ("%d", I);     return 0; }

the implementation is from small to large sort. But we asked for the order from the big to the small, just to change for (i=0;i<=10;i++) to for (i=10;i>=0;i--), OK 。


Complexity of Time:

The loop of line 5th in the code has a total cycle of M (M is the number of buckets), and the code for line 7th loops n times (n is the number of rows to be sorted), and the 12th and 13th lines loop m+n times altogether. Therefore the entire sort algorithm carried out altogether m+n+m+n times. We use the capital letter O to represent the time complexity, so the time complexity of the algorithm is O (m+n+m+n) i.e. O (m+n). We can ignore the smaller constants when we talk about time complexity, and the time complexity of the final bucket sequencing is O (m+n). Also, in the case of time complexity, N and M are usually in uppercase letters O (m+n).


This is a very fast sorting algorithm.


Disadvantages :

Very wasted space ! For example, the range that needs to be sorted is between 0~2100000000, then you need to apply 2,100,000,001 variables, which means to write int a[2100000001]. Because we need to use 2,100,000,001 buckets to store the number of occurrences of each of the 0~2100000000 between the two. Even if you have only 5 numbers to sort (for example, these 5 numbers are 1, 1912345678, 2100000000, 18000000, and 912345678), you still need 2,100,000,001 buckets, which is a waste of space! Also, if you need to sort now is no longer an integer but some decimals, such as the 5.56789, 2.12, 1.1, 3.123, 4.1234 this five number from small to large sorting and how to do? Bucket sorting does not solve decimal sort .

Bucket sort [fastest and simplest sort]

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.