Classic Sort algorithm-count sort counting sort
Note that this is two different sorts, as distinguished from the cardinal sort
The process of counting the sort is similar to the process of selecting a class cadre in a primary school, such as a person 10 votes, the author 9 votes, that person is the monitor, the author is the deputy Monitor
The first part is to pull the ballot and vote, the second part is based on your votes into the bucket
Look at the specific process, a total of three arrays are required, namely the array to be queued, the ballot boxes array, and the bucket array
var unsorted = new int[] {6, 2, 4, 1, 5, 9}; Array to be queued
var ballot = new int[unsorted. Length]; Ballot boxes Array
var bucket = new int[unsorted. Length]; Bucket array
Finally, look at the bucket array, the array of rows and the ballot boxes first
Initial state, iteration variable i = 0 o'clock, pending array [i] = 6, ballot box array [i] = 0, which establishes a link between a number and its bucket number (that is, the number of votes) by iterating over the variable
Array to be queued [ 6 2 4 1 5 9 ] i = 0 o'clock, can be removed from the array to be queued 6
Array of ballot boxes [ 0 0 0 0 0 0] You can also remove 6 of the votes from the box array, that is, the bucket number
The process of drawing votes
First of all, 6 out of the ballot, 6 of the ballot is No. 0, 6 for all the other numbers said, who is smaller than me or equal to me, vote for me, or beat you
So, 2 4 1 5 to 6 votes, put in ballot No. 0, 6 to four votes
arrays to be queued [6 2 4 1 5 9]
Ballot boxes Array [4 0 0 0 0 0]
The next 2 began to pull the ballot, said to others, who is smaller than me, who vote for me, or else to get you! So 1 cast a vote, other people than 2 big ignore, thought you can really two
So 2 from 1 that got a vote
arrays to be queued [6 2 4 1 5 9]
Ballot boxes Array [4 1 0 0 0 0]
And then yes,
4 votes of 2 and 1, total two votes
1 got 0 votes, no one voted for him.
5 got three tickets for the 2,4,1 vote.
9 is the largest, get everyone (excluding themselves) vote, total 5 votes (array length-1 votes)
This is the state when the polls are complete.
arrays to be queued [6 2 4 1 5 9]
Ballot boxes Array [4 1 2 0 3 5]
The process of entering the barrel
The voting process is over, everyone has their own votes, the bucket array says, watch your own votes, enter the same number of votes in the bucket , go
6 Total 5 votes, enter 5th barrels
2 Get 1 votes, enter 1th barrels, a few votes into the number of barrels
42 votes, 2nd barrels, 53 votes into 3rd barrels, 9 5 votes, and 5th barrels.
arrays to be queued [6 2 4 1 5 9]
Ballot boxes Array [4 1 2 0 3 5]
-----------------------
Before the barrel [ 0 1 2 3 4 5]//Inside the number indicates the bucket number
After entering the barrel [ 1 2 4 5 6 9]//1 has 0 votes, into the No. 0 barrels
Sorted, sequential output can be [1 2 4 5 6 9]
As you can see, the larger the number, the more the 9 get the votes of everyone except yourself, 5 votes, the most votes, so 9 the largest,
Everyone has the [array length minus own] ticket
1 votes minimum, so 1 is the smallest number,
The counting sort simultaneously has the high efficiency and the fast platoon of the barrel platoon overbearing,
The completion code is as follows
var unsorted = new int[] {6, 2, 4, 1, 5, 9}; Array to be queued
var ballot = new int[unsorted. Length]; Ballot boxes Array
var bucket = new int[unsorted. Length]; Bucket array
for (int i = 0; i < buckets. Length; i++)
{
Unsorted[i] = 6
Ballot[i] is a 6 ballot, with 4 tickets inside.
Bucket[ballot[i]] = unsorted[i];
Bucket[4 ticket] = 6;
Bucket[ballot[i]] = unsorted[i];
}
for (int i = 0; i < buckets. Length; i++)
{
Console.WriteLine (Bucket[i]);
}
Classic Sort algorithm-count sort counting sort