Sorting algorithm cardinal Order: LSD and msd__ sorting algorithm

Source: Internet
Author: User
Tags array length
1. Algorithm principle

Cardinality sorting is done through the "assign" and "collect" procedures.

1) allocation, first from a single-digit start, according to the bit value (0-9), respectively put into the 0~9 bucket (such as 53, Single-digit 3, then put into the 3rd bucket)

2) collection, and then put the data placed in the 0~9 bucket in order into the array

Repeat (1) (2) process, from single to highest (such as 32-bit unsigned maximum number 4294967296, highest bit 10 digits). And how is this thought to be understood? Take a look at the following examples.

(1) The false set of data sequence is as follows:

73 28 93 43 55 14 22 65 26 81

First, according to the number of single-digit digits, when traversing the data, they are assigned to the bucket numbered 0 to 9 (single-digit value corresponds to barrel number one by one).

Distribute the collection process from low to High:

As you can see, the original unordered data sequence is sorted. If the sorted data series has more than three digits of data, repeat the above action until the maximum number of digits.
Based on two different sort orders, we will sort the cardinality into

LSD(least significant digital): Sort by the rightmost (low) value.

MSD(Most significant digital): begins at the leftmost (high) value.

One thing to note:

The cardinal order of LSD is suitable for the number of digits, and if there are more digits, the efficiency of using MSD is better.

The MSD method is distributed by the base of the high number, instead of merging back into an array immediately after the assignment, the "sub bucket" is created in each bucket, and the values in each bucket are assigned to the sub bucket by the value of the next digit. Merges back into a single array after the lowest-digit allocation is completed.

Distribute the collection process from high to Low:

73 28 93 43 55 14 22 65 26 81


(2) We consider the sort of poker as a sort of primary keyword consisting of two items of color and face value. Requirements are as follows:

Color Order: Plum < Diamonds < Hearts < spades

Denomination Order: 2<3<4<...<10<j<q<k<a

Then, to arrange a deck of cards in the following order:

Plum Blossom 2, ..., clubs A, squares 2, ..., Box A, Hearts 2, ..., Hearts A, spades 2, ..., spades a.

There are two kinds of sorting methods:

<1> by the suit into four piles, to collect each heap, and then to each pile by the face value from small to large arrangement, and then by the color of the pile by stacking up. ----known as the "highest priority" (MSD) method.

<2> in the first order from small to large into 13 piles, and then collected from childhood to large, and then according to different colors into four piles, the final sequence of collection. ----known as the "least-bit first" (LSD) method.

2. Algorithm Analysis

Average time complexity: O (DN) (d is the maximum number of digits to be shaped)

Space complexity: O (10n) (10 for 0~9, for storing temporary sequences)

Stability: Stability

3. Program Implementation

(1) LSD method to achieve

The lowest-bit precedence method first sorts all objects based on the lowest bit key KD,

Again according to the secondary low key code Kd-1 The result of the last trip sort,

Repeat sequentially until the last order of the key code K1 is completed, and an ordered sequence can be obtained.

When you sort each key by using this sort method, you do not need to group, but the entire group of objects.

Because of the allocation and collection phase, numbers conform to first-in-first-out relationships. Therefore, you can use 10 queues to save the number assigned on 0-9, in the collection phase, the first in first out in order to remove the number in each bucket, and then put in the original array.

/********************************************************
* Function Name: Getnuminpos
* parameter description: num a cosmetic data
*     POS indicates the first POS bit data to be obtained
* Description:    find Num's from low to high POS bit data
***************************************************** /
int getnuminpos (int num, int pos)
{
	int temp = 1;
	for (int i = 0; i < pos-1 i++)
		temp *=;

	Return (num/temp)%;

#define MAXPOS    //highest bit
void Radixsort (vector<int> &a)
{
	int len = A.size ();
	Vector<vector<int>> Radixarray (a);  Divided into 0~9 sequence space for

	(int pos = 1; pos <= maxpos pos++)    //from single-digit to highest digits {for
		(int i = 0; i < Len; i++ )    //allocation process
		{
			int num = Getnuminpos (a[i], POS);
			Radixarray[num].push_back (A[i]);

		for (int i = 0, j = 0; i < i++)    //collect {while
			(!radixarray[i].empty ())
			{
				a[j++] = Radixarra Y[i].front ();  The header data is inserted into the original array
				radixarray[i].erase (Radixarray[i].begin ()).    Remove header Element}}}
(2) Realization of MSD method

The highest bit precedence is usually a recursive process:

<1> first, according to the highest bit key K1 sorting, get several object groups, each object in the object group has the same key code K1.

<2> the objects in each group are sorted according to the key code K2, according to the K2 value, and then divided into several smaller subgroups, each of which has the same K1 and K2 values.

<3> repeat until you have sorted the key KD.

<4> Finally, the objects in all subgroups are connected sequentially, and an ordered sequence of objects is obtained.

/********************************************************
* Function Name: Getnuminpos
* parameter description: num a cosmetic data
* POS Represents the POS bit data for the shape to be obtained
* Description: Find num's from low to high POS bit data
*********************************************************/
int getnuminpos (int num, int pos)
{
int temp = 1;
for (int i = 0; i < pos-1 i++)
temp *= 10;
 return (num/temp)% 10;} MSD, the maximum number of digits D void Radixsort (vector<int> &a, int d) { int len = a.size () is specified at the time of invocation;  vector<vector<in T>> Radixarray (Ten); //is divided into 0~9 sequence space, with the queue to save each bucket allocated data 
The number of digits is greater than 0, and the array length is greater than 1
if (d >= 1 && len > 1)
{for
(int i = 0; i < len; i++)//allocation process
{   
int num = Getnuminpos (a[i], d);
Radixarray[num].push_back (A[i]);
}
cout << Endl;
for (int i = 0, j = 0; i < i++)//collect
{
radixsort (radixarray[i], d-1);  Recursively, assigning a while
(!radixarray[i].empty ())
{
a[j++] = Radixarray[i].front () to each child barrel from the secondary high.   Take the queue header data into the original array
radixarray[i].erase (Radixarray[i].begin ());
}
}
}
}


Reference: http://blog.csdn.net/cjf_iceking/article/details/7943609

Http://www.cnblogs.com/Braveliu/archive/2013/01/21/2870201.html
   

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.