Sort Cardinal sort (count sort, bucket sort)

Source: Internet
Author: User

Here are two ways to make a bucket sort:

A. Simplified bucket sequencing

The code is as follows:

<span style= "FONT-SIZE:18PX;" >/* simplified version of bucket sort */#include <stdio.h>int main () {int book[1001],i,j,t,n;for (i=0;i<=1000;i++) {book[i]=0;} scanf ("%d", &n);//Enter a number n, indicating that there is n number for (i=1;i<=n;i++)//loop read n number, and the bucket sort {scanf ("%d", &t);//read each number into the variable t [t]++; Count, place a small flag on the bucket numbered T}for (i=0;i<=1000;i++)/////////////////////////////To print the bucket number several times (j=1;j<=book[i];j++) printf ("%d", I);} GetChar ();} GetChar (); return 0;} </span>


Two. Full bucket sorting

Base sort (with MSD and LSD)

now to achieve the simplest cardinal sort, that is, the numbers only from small to large sort, there is no category of points.
The idea of a cardinal sort is: sort the digits first, and after the sort is done, count the piles.
Then sort the ordered 10-digit number above, sort it out, then sort the ordered hundred numbers, and so on.

The function plus test code is as follows:

/* Full Bucket sort * * #include <iostream> #include <list>using namespace std; #define RADIX 10/** * Base tree sort (with MSD and LSD) * Now realize the simplest cardinal sort, that is, the number only from small to large sort, no category of the idea of the base sort * is: * First digit of the order, after the sorting is complete, the statistics piles * and then on the top of the order of the 10-digit number sorted, after the completion of the order, then the order of the hundred numbers sorted, An analogy */void radixsort (int arr[],int n) {for (int m = 0;m < radix;m++)//Create RADIX list {//or use list as        Barrel list<int> *lists = new list<int>[radix];            for (int i = 0;i < n;i++) {int temp = Arr[i];int loc = 1;            for (int t = 1;t < m;t++) {loc *= 10;                                 } lists[(temp/loc%10)].push_back (Arr[i]);//The same value is inserted into the equivalent list} int j = 0; Merge radix list for (i = 0;i < 10;i++) {if (lists[i].size ()! = 0) {list<int>::i                Terator it = Lists[i].begin ();                    while (It! = Lists[i].end ()) {arr[j] = *it;                    it++;            j + +;    }}}}}void main () {int arr[] = {278,109,63,930,589,184,505,269,8,83};for (int i = 0;i < sizeof (arr)/sizeof (int); ++i) {cout<<arr[i]<< "";} cout<<endl; Radixsort (arr,sizeof (arr)/sizeof (int)); for (i = 0;i < sizeof (arr)/sizeof (int); ++i) {cout<<arr[i]<< "";} Cout<<endl;}


Running results such as:




I am very much looking forward to your criticisms and suggestions ~ Thank you all

Sort Cardinal sort (count sort, bucket 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.