C++ implementation of the radix sort method

  • 2020-04-02 00:58:35
  • OfStack

Radix sort is a noncomparative integer sorting algorithm , which works by cutting integers into digits and then comparing them separately. Since integers can also represent strings (such as names or dates) and floating-point Numbers in a particular format, radix sort is not restricted to integers. The invention of radix sort can be traced back to the contribution of hermann holley to the punched card Tabulation Machine in 1887.
It works like this: The Numbers to be compared (positive integers) are unified into the same digit length, and the shorter digits are zeroed in front.
Radix sort can be done in a way that is LSD (Least significant digital) or MSD (Most significant digital), with LSD starting at the rightmost of the key and MSD starting at the leftmost of the key.
(from wikipedia)
The following is my own realization, shortcomings, but also hope to point out:

//RadixSort. CPP: defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
using namespace std;
//Define the node of 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, and the program needs to create it
 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 that already exists
 void push(Node* p)
 {
  p->next = NULL;
  rear->next = p;
  rear = p;
 }
 //The largest number of digits in a data element
 int lenData()
 {
  int temp(0);//The maximum number of bits of a data element
  int n(0);   //The number of bits that a single data element has
  int d;      //To store data elements to be compared
  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;
 }
 //Determines if the queue is empty
 bool empty()
 {
  if (front == rear)
  {
   return true;
  }
  return false;
 }

 //Clear the elements in the queue
 void clear()
 {
  front->next = NULL;
  rear = front;
 }

 //Output the elements in the queue
 void print(Queue& que)
 {
  Node* p = que.front->next;
  while (p != NULL)
  {
   cout << p->data << " ";
   p = p->next;
  }
 }

 //Radix sort
 void RadixSort(Queue& que)
 {
  //Defines an array of Pointers to ten queues
  Queue* arr[10];
  for (int i = 0; i < 10; i++)
  {
   arr[i] = new Queue;
  }
  int d = 1;
  int m = que.lenData(); //Gets the maximum number of bits in the data element to be sorted

  //Assign elements from the initial queue to ten queues
  for(int i = 0; i < m; i++)
  {
   Node* p = que.front->next;
   Node* q;
   int k;  //If the remainder is k, it is stored in the queue pointed to by arr[k]
   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 ten queues into the original queue
   for (int i = 0; i < 10; 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 < 10; i++)//Clear ten queues
   {
    arr[i]->clear();
   }
   d *= 10;
  }
  print(que); //The ordered elements in the output queue
 }
private:
 Node* front;
 Node* rear;
};
int _tmain(int argc, _TCHAR* argv[])
{
 Queue oldque;
 int i;
 cout << "Please input the integer numbers you 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 without careful analysis

#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;
}


Related articles: