Count sort & Bucket sort & Cardinal sort

Source: Internet
Author: User

Why write such a drop a blog pinch ... Because a new head asked a water problem, the result is inexplicably caused the fight.

Then suddenly found that the bucket sort before understanding is not the real bucket sort, so write an article to distinguish the three very similar sort spicy.

The awakening of the old food rabbit!!!

Count Sort

Counting sorting is a fast sorting algorithm, but it takes a lot of space.

The specific operation:

For example, to a sequence like this: 6 9 3 2 3 5

I need an array a[i] indicates how many of the numbers in the series are

This allows O (n) to process the array

  read (x);  A[X]+ +;

For example, an array of spicy chestnuts is such a drop

I 1 2 3 4 5 6 7 8 9
A[i] 0 1 2 0 1 1 0 0 1

And then this array has nothing to pinch

It can be found that only enumeration I (e.g. Li Zili is 1≤i≤9) is required

Then output A[I] I was after a small to large sequence

 for i=19 does (usually can take the maximum number in the series Max, which takes the Chestnuts 9  ) forj=1   Do   Write (i,");

After that, it will output 2 3 3 5 6 9

This is the sort of notation (old-age food rabbit before putting the count sort of mistaken for a bucket sort qaq)

Efficiency is basically O (N) But there is a flaw, that is, when the value of the series is very large, the array will not open.

This problem how to solve good pinch, is the bucket sort and base sort spicy!

Bucket Sort

The bucket sort is actually some optimization of the sort of counting, and he turns the time back into a part of the space (counting sort with space for time).

Barrel sort what is the idea of pinching?

go ahead and raise a chestnut. 6 9 3 2 3 5 (gee, I'm familiar)

First I want to set a value m this value can be arbitrarily fixed, but it will affect the efficiency.

What is M used for?

So to understand, the counting sort is actually using a lot of a lot of buckets total max

The bucket sort is used (max/m) buckets so the meaning of this m is actually a range of intervals.

Counting sorting is a special case of bucket ordering, that is, when taking m=1.

Then I need an array a[i,j] represents the value of the J element in the first bucket.

(usually not arrays, but linked lists, because it is possible to have such data, such as the sequence is all 1-3 range of this time the array will not open, if n is very large)

Well, make a list.

Here I take m=3 (that is, to give a chestnut)

Range of data in buckets The 4~6 7~9
corresponding I (i.e. the first few barrels) 1 2 3
Elements inside a bucket 3,2,3 6.5 9

At this time found what pinch ... The elements in each bucket are unordered.

So do a different sort of every bucket, like a quick row.

And then sort it out and merge the buckets together.

Eh??? Every barrel? is not the efficiency of spicy is very low.

The answer is no, in contrast, buckets are usually more efficient than fast.

The average efficiency of the fast row is O (n log n) and the bucket pinch is O (max/m *m log m) i.e. O (max log m) hypothesis max=n=1000000 (1 million)

The calculation of the fast row is about 23000000 (23 million) and the calculation of the bucket is about 12000000 (12 million) if it takes m=2500 (which saves a bit of space and has a high time efficiency).

The sorting efficiency of buckets is also high.

and the sorting algorithm for each bucket of buckets can also be changed to other not necessarily fast rows.

The bucket sort app doesn't seem to be much, and it seems that a lot of people confuse the cardinal sort with the bucket sort.

By contrast, the number of people who mistakenly think of a cardinal sort is more like a barrel sort.

Therefore, the application of radix sorting should be wider.

Base Sort

The method of Cardinal sort is more magical, he uses the idea of counting sort.

Operation of Cardinal sort I'm still going to raise a chestnut ... but the picture's not going to be qaq, or the water's a little big .

such as: 543 123 756 666 841 322 10 799 69 (Finally, I changed a chestnut, because the last time I lost a chestnut)  

The operation of the cardinality sort is this.

The lowest bit (digit) is a keyword, and the second low is a keyword ... And so on

I first to the lowest bit for the keyword to do a count sort.

If you still use a bucket analogy, because there will only be 0-9 of these numbers on a single digit.

So it's 10 barrels.

Make a list.

barrels 0 1 2 3 4 5 6 7 8 9

After 1th operation

Elements in the bucket

10 841 322 543,123     756,666     799,69

And then the first operation and then merging together is such a series (note that each bucket is unordered, is the position of the original sequence)

10 841 322 543 123 756 666 799 69

or an unordered sequence of numbers, followed by a sequence of key words for the second-lowest.

Barrel 0 1 2 3 4 5 6 7 8 9

After the 2nd time operation

The elements in the bucket

10 322,123 841,543 756 666,69 799

Then the second operation and then merge together is such a series (note that each bucket is still unordered, is the first operation after the position of the sequence)

10 322 123 841 543 756 666 69 799

And then we sort the third-low for the keyword.

Barrel 0 1 2 3 4 5 6 7 8 9

After the 2nd time operation

The elements in the bucket

010,069 123 322 543 666 756,799 841

Then merge

10 69 123 322 543 666 756 799 841

At this point the sort ends and the sequence is ordered.

Isn't that a magical pinch?

Why do you want to count each bit once?

is actually changing the position. For example, if there are 123 124 such elements, in the original sequence is such 124 123

What is spicy? Counting the lowest bits can be turned into 123 124, which changes the position.

Okay, spicy cardinal sort of efficiency?

Set the maximum value to have a D bit

Approx. O (d*n) efficiency

and d is very small, compared to the barrel sort, although the efficiency is reduced a little, but for some very large data.

and expand a little bit of the base row

In fact, the use of 10 barrels here, spicy can you use more barrels pinch? Of course

I can use 100 barrels, talking about the lowest bit and sub-low as a whole as a keyword, such as 5678 at this point 78 is a keyword 56 is another keyword.

Then the operation is the same, but only d/2 a keyword, and thus efficient and fast, but the space has become more, into 100 barrels.

And so on, there are 1000 10000 ... A bucket. Spicy if not the whole 10 barrels of the line, of course, so the words need to see the number of 10 in the number of other binary to do the count sort. It's not very detailed.

OK ~ Finished, summed up, overall, three sorts are used to count the idea of sorting.

Buckets are highly efficient, but they are too large to be used, and cardinality sorting is not only much more efficient, but also applies to large numbers of data.

The end of the explanation of the old-age food rabbit ~ Sprinkle flower ~

Count sort & Bucket sort & Cardinal 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.