Sort the distribution of special topics

Source: Internet
Author: User
Tags sort sorts

The allocation sort is divided into two pieces, the bucket sort and the cardinal sort, and the cardinal sort is the progressive of the barrel sort:

Here's a look at these two sorts:



To put it simply, the bucket sort is to count the numbers in sequence,
It then iterates through the starting point of each number (the same number has been merged), and then
To scan forward from the tail, a sorted sequence is ordered:
The specific code is as follows:


#include <iostream>
using namespace std;
int main ()
{
	int n;
	cin>>n;
	int s[10],a[10];
	int count[50];
	int Max; 
	int t;
	int i,j;
	for (i=0;i<n;i++)
	{
		cin>>s[i];
		A[i]=s[i];
	}
	for (i=0;i<50;i++)
	{
		count[i]=0;
	}
	
	for (i=0;i<n;i++)
	{
		count[s[i]]++;
	}
	for (i=0;i<50;i++)
	{
		count[i]=count[i]+count[i-1];
	}
	for (i=n-1;i>=0;i--)
	{
		s[--count[a[i]]]=a[i];
	}
	for (i=0;i<n;i++)
	{
		cout<< "" <<s[i];
	}
	return 0;
}

Base sort:




Multi-Keyword Sorting method
Highest-bit precedence method (MSD): Sorts the highest-bit keyword K1 (such as a suit) first, dividing the sequence into sub-sequences,
Each subsequence has the same K1 value, and then each subsequence sorts the secondary keyword K2, such as the face value,
It is divided into smaller sub-sequences, and repeats sequentially until the lowest bit keyword KD is ordered for each sub-sequence;
Finally, all the sub-sequences are concatenated together to form an ordered sequence.





Lowest bit precedence method (LSD): Sorts from the lowest-bit keyword kd, then sorts the higher-bit keywords,
...... Repeats sequentially until the highest bit keyword K1 is sorted to become an ordered sequence
Different characteristics of MSD and LSD
Sort by MSD, you must divide the sequence into several sub-sequences by layer, and then sort each of the sub-sequences separately
Sort by LSD, without dividing it into sub-sequences, for each keyword is the whole sequence to participate in the sort
and sorted by several allocations and collections, not by keyword comparisons

Order method for Cardinal ordering (D-bucket sort)


for (i=1;i<=d;i++)
	{for
		(j=0;j<r;j++)//Bucket 0 
		{
			count[j]=0;
		}
		for (j=0;j<n;j++)//Bucket count 
		{
			k= (s[i]/ra)%r;
			count[k]++;
		}
		for (j=1;j<r;j++)
		{
			count[j]=count[j]+count[j-1];//Statistics 
		} for
		(j=n-1;j>=0;j--)// The reverse of the bucket sort is put into 
		{
			k= (s[j]/ra)%r;
			count[k]--;
			A[COUNT[K]]=S[J];
		}
		for (j=0;j<n;j++)//re-give assignment into the second bucket sort 
		{
			s[j]=a[j];
		}
		ra*=r;//change mode, move left one 
	}




Chain cardinality sorting
Cardinal sort: One way to sort single-logical keywords with assign and collect
Chain Cardinal Sort: The base sort of the storage structure with the linked list




Chain Cardinal Sort Step
Set 10 queues, F[i] and E[i] respectively the head and tail pointers for the I-queue
The first trip is assigned to the lowest bit key (single digit), changing the value of the recorded pointer,
Assign records in a linked list to 10 chain queues, with the same digit for each queue record
The first trip collection is a pointer field that changes the tail record of all non-empty queues.
Make it point to the next non-empty queue's team header record, and re-chain the 10 queues into a linked list
Repeat the above two steps, the second trip, the third trip allocation and collection, respectively, 10, Hundred,
Finally get an ordered sequence


void distribute (record *a[],int first,int i,int r,staticqueue *queue)//allocation process 
{
	int j,k,a,curr=first;
	for (j=0;j<r;j++)//Initialize entire queue 
	{
		queue[j].head=-1;
	}
	
	while (CURR!=-1)//Assign the entire static chain
	{
		k=a[curr].key;//Take the first bit of sort code number K for
		(a=0;a<i;a++)
		{
			k/=r;
		} 
		K%=r;
		if (queue[k].head==-1)//Bucket is empty, then A[curr] is the first record 
		{
			queue[k].head=curr;
		}
		else A[queue[k].tail].next=curr;
		Queue[k].tail=curr;
		Curr=a[curr].next;	
	} 
}


void Collect (Record *a,int &first,int r,staticqueue *queue)//collection process
{
	int last,k=0;
	while (QUEUE[K].HEAD==-1)//Find the first non-empty queue 
	{
		k++;
	}
	First=queue[k].head;
	Last=queue[k].tail;
	while (K<R-1)//continue to collect the next queue 
	{
		k++;
		while (K<r-1&&queue[k].head==-1)
		{
			k++;
			if (queue[k].head!=-1)
			{
				a[last].next=queue[k].head;
				Last=queue[k].tail;
			}
		}
		A[last].next=-1; Collection completed 
	} 
	
	
	





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.