C + + Implementation of the method of cardinal order detailed _c language

Source: Internet
Author: User
Cardinality ordering (radix sort) is a non-comparative integer sort algorithm, the principle is to cut the integer by the number of digits to a different number, and then compare each digit by the number of digits. Because integers can also express strings (such as names or dates) and floating-point numbers in a particular format, cardinality sorting is not only used for integers. The invention of Cardinal order can be traced back to the contribution of Herman He Leri in punching card watchmaking (tabulation Machine) in 1887.
That 's how it 's done:Unify all values to be compared (positive integers) to the same number of digits, preceded by a short number of 0. Then, start at the lowest bit, and then sort it one at a time. So that from the lowest bit to the highest order, the series becomes an ordered sequence.
Cardinality sorting can take the form of LSD (least significant digital) or MSD (Most significant), and the sort of LSD starts at the far right of the key value, and digital, on the other hand, starting at the far left of the key value.
(The above is transferred from Wikipedia)
Here is my own implementation, deficiencies, but also to point out:
Copy Code code as follows:

RadixSort.cpp: Defines the entry point for a console application.
#include "stdafx.h"
#include <iostream>
using namespace Std;
Define the node for the queue
struct Node
{
int data;
Node* Next;
};
Define the special queues required by the program
Class Queue
{
Public
Queue ()
{
node* p = new Node;
P->data = NULL;
P->next = NULL;
Front = p;
Rear = P;
}
~queue ()
{
node* p = Front;
node* Q;
while (p)
{
Q = p;
p = p->next;
Delete q;
}
}
Add an element to the end of the queue, the node does not exist, requires program creation
void push (int e)
{
node* p = new Node;
P->data = e;
P->next = NULL;
Rear->next = p;
Rear = P;
}
Add a node to the end of the queue.
void push (node* p)
{
P->next = NULL;
Rear->next = p;
Rear = P;
}
Maximum number of digits in a data element
int Lendata ()
{
int temp (0);//Data element maximum number of digits
int n (0); The number of digits that a single data element has
int D; Used to store data elements for comparison
node* p = front->next;
while (P!= NULL)
{
D = p->data;
while (d > 0)
{
D/= 10;
n++;
}
p = p->next;
if (Temp < n)
{
temp = n;
}
n = 0;
}
return temp;
}
To determine whether a queue is empty
BOOL Empty ()
{
if (front = = rear)
{
return true;
}
return false;
}

Clear the elements in a queue
void Clear ()
{
Front->next = NULL;
Rear = front;
}

Elements in the output queue
void print (queue& que)
{
node* p = que.front->next;
while (P!= NULL)
{
cout << p->data << "";
p = p->next;
}
}

Cardinality sort
void Radixsort (queue& que)
{
Defines an array of pointers that contain 10 pointers to 10 queues, respectively.
queue* ARR[10];
for (int i = 0; i < i++)
{
Arr[i] = new Queue;
}
int d = 1;
int m = Que.lendata (); Get the maximum number of digits in the data element to be sorted

Assign elements in the initial queue to 10 queues
for (int i = 0; i < m; i++)
{
node* p = que.front->next;
node* Q;
int k; The remainder is k, stored in the queue that arr[k] points to
while (P!= NULL)
{
K = (p->data/d)%10;
Q = p->next;
Arr[k]->push (P);
p = q;
}
Que.clear (); Empty the original queue

Collect data from 10 queues into the original queue
for (int i = 0; i < i++)
{
if (!arr[i]->empty ())
{
node* p = arr[i]->front->next;
node* Q;
while (P!= NULL)
{
Q = p->next;
Que.push (P);
p = q;
}
}
}
for (int i = 0; i < i++)//Empty 10 queues
{
Arr[i]->clear ();
}
D *= 10;
}
print (que); Sorted elements in output queues
}
Private
Node* Front;
node* Rear;
};
int _tmain (int argc, _tchar* argv[])
{
Queue Oldque;
int i;
cout << "Please input the integer numbers your want to sort. Input Ctrl+z to the end: << Endl;
while (CIN >> i)
{
Oldque.push (i);
}
Oldque. Radixsort (Oldque);
cout << Endl;
return 0;
}

The following code has been transferred from Wikipedia, not yet carefully analyzed, first.
Copy Code code as follows:

#include <iostream>

using namespace Std;

const int base=10;

struct WX
{
int num;
WX *next;
WX ()
{
Next=null;
}
};

WX *headn,*curn,*box[base],*curbox[base];

void Basesort (int t)
{
int i,k=1,r,bn;
for (i=1;i<=t;i++)
{
K*=base;
}
R=k*base;
for (i=0;i<base;i++)
{
Curbox[i]=box[i];
}
for (Curn=headn->next;curn!=null;curn=curn->next)
{
Bn= (curn->num%r)/k;
curbox[bn]->next=curn;
curbox[bn]=curbox[bn]->next;
}
CURN=HEADN;
for (i=0;i<base;i++)
{
if (Curbox[i]!=box[i])
{
curn->next=box[i]->next;
Curn=curbox[i];
}
}
curn->next=null;
}

void Printwx ()
{
for (Curn=headn->next;curn!=null;curn=curn->next)
{
cout<<curn->num<< ';
}
cout<<endl;
}

int main ()
{
int i,n,z=0,maxn=0;
Curn=headn=new WX;
cin>>n;
for (i=0;i<base;i++)
{
Curbox[i]=box[i]=new WX;
}
for (i=1;i<=n;i++)
{
Curn=curn->next=new WX;
cin>>curn->num;
Maxn=max (Maxn,curn->num);
}
while (maxn/base>0)
{
Maxn/=base;
z++;
}
for (i=0;i<=z;i++)
{
Basesort (i);
}
PRINTWX ();
return 0;
}

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.