Sort by base and count

Source: Internet
Author: User

Sort by base and count

Base sort

I,What is a base?

In mathematics, cardinal number is a concept in set theory that depicts any set size. Two sets that can establish one-to-one correspondence between elements are called Peer-to-Peer sets. Sets are classified based on the peering relationship. Any set that is equal to each other is classified into the same class. In this way, each set is classified into a certain category. The class to which any set A belongs is called the base number of Set A, which is recorded as | A | (or cardA ). In this way, when A and B belong to the same class, A and B have the same base, that is, | A | = | B |. When A and B belong to A class, their base numbers are also different.

 

II,Base sort

Radix sort is a distributed sort ). Sort allocation. Here we will talk about sorting in five ways based on the ideas or methods used.Exchange sorting(Bubble sorting and quick sorting ),Insert sort(Insert sorting directly and sort by Hill ),Select sort(Directly select sorting and heap sorting ),Merge Sorting,Sort allocation(Bucket sorting and base sorting ).

The base sorting is the optimization of the general sorting. Compared with the general sorting, the base sorting uses fewer buckets, and the number of buckets used in the general sorting depends on the actual situation, generally, the number of buckets used in the base sorting is the same as that in the allocated set. Generally, the number of buckets is small. For example, if we sort the n elements belonging to [1, 1000], we will use buckets. If it is a base sorting, we will only use 10 buckets.

The bucket is also called the bucket sub-method (bucket sort or bin sort). As the name suggests, it refers to some information about the key value, elements to be sorted are allocated to certain "buckets" to achieve sorting. The base sorting method is a stable sorting, and its time complexity is O (nlog (r) m ), r indicates the base number adopted, while m indicates the number of heaps. In some cases, the base sorting method is more efficient than other stability sorting methods.

 

III,Sort Drill

For example, we sort the ten numbers {11, 41, 24, 39, 35.

First, let's proceedFirst allocation, The selected base number is the number on a single digit, And the number on a single digit belongs to [], so we create ten buckets and place these numbers in these ten buckets.

The first bucket, with a single digit of 0 :{}

The second bucket, with a single digit of 1: {11, 41}

The third bucket, with a single digit of 2: {42}

The fourth bucket, with a single digit of 3 :{}

Fifth bucket, with a single digit of 4: {24}

The sixth bucket, with a single digit of 5: {35, 15}

The seventh bucket, with a single digit of 6: {26, 36}

Eighth bucket, with a single digit of 7 :{}

The ninth bucket, with a single digit of 8: {78}

Tenth bucket, with a single digit of 9: {39}

After the allocation is complete, I will reclaim the data into a set in the order of buckets.

}

At this time, we find that the numbers are incremental. Next we can continue to allocate the previous (10. Make ten buckets.

The first bucket, with ten digits being 0 :{}

The second bucket, with ten digits being 1: {11, 15}

The third bucket, with 10 digits being 2: {24, 26}

The fourth bucket, with 10 bits being 3: {35, 36, 39}

The fifth bucket has four digits}

The sixth bucket, 10 is the number of 5 :{}

The seventh bucket, with 10 in 6 :{}

The eighth bucket, 10 is 7: {78}

The ninth bucket, with ten digits being 8 :{}

The tenth bucket, 10 is the number of 9 :{}

After the allocation is complete, I will reclaim the data into a set in the order of buckets.

{, 24, 26, 35, 36, 39,41, 78 ,}

At this time, the sorting is completed.

 

IV,JavaCode Implementation

 

Public ClassRadixSort {

Public Static VoidMain (String [] args ){

Int[] S = };

Radix(S, 2 );

For(IntI: s)

System.Out. Print (I + "");

}

Public Static VoidRadix (Int[] S,IntDistance ){

Int[] [] S2 =New Int[10] [s. length]; // create a bucket

Int[] Size =New Int[10]; // record the number of data contained in each bucket

IntBit = 1; // represents a bit (single digit, ten digits, hundreds of digits ,,,)

While(Distance> 0 ){

// Clears the number of data in each bucket

For(IntI = 0; I <10; I ++)

Size [I] = 0;

// Step 1: first divide the data into buckets

Bit * = 10;

For(IntI = 0; I <s. length; I ++ ){

IntIndex = s [I] % bit/(bit/10); // obtain the number of corresponding bits

S2 [index] [size [index] = s [I];

Size [index] ++;

}

// Step 2: store the data in each bucket in the original array.

IntIndex = 0;

For(IntI = 0; I <s2.length; I ++ ){

For(IntJ = 0; j <size [I]; j ++)

S [index ++] = s2 [I] [j];

}

Distance --;

}

}

}

 

Running results: 11 15 15 25 43 45 46 48 48

V,Application of base sorting

Base sorting is suitable for sorting elements that contain multiple basic features, such as time, year, month, and day. We find that it is too difficult for other sorting methods to sort the time, the base sorting is easy to complete. sort by day, then sort by month, and sort by year.

 

 ---------- Yan huijie

Related Article

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.