C++實現(xiàn)基數(shù)排序的方法詳解
基數(shù)排序(Radix sort)是一種非比較型整數(shù)排序算法,其原理是將整數(shù)按位數(shù)切割成不同的數(shù)字,然后按每個位數(shù)分別比較。由于整數(shù)也可以表達字符串(比如名字或日期)和特定格式的浮點數(shù),所以基數(shù)排序也不是只能使用于整數(shù)?;鶖?shù)排序的發(fā)明可以追溯到1887年赫爾曼·何樂禮在打孔卡片制表機(Tabulation Machine)上的貢獻。
它是這樣實現(xiàn)的: 將所有待比較數(shù)值(正整數(shù))統(tǒng)一為同樣的數(shù)位長度,數(shù)位較短的數(shù)前面補零. 然后, 從最低位開始, 依次進行一次排序.這樣從最低位排序一直到最高位排序完成以后, 數(shù)列就變成一個有序序列.
基數(shù)排序的方式可以采用LSD(Least significant digital)或MSD(Most significant digital),LSD的排序方式由鍵值的最右邊開始,而MSD則相反,由鍵值的最左邊開始。
(以上轉自維基百科)
下面是我自己的實現(xiàn),不足之處,還望指正:
// RadixSort.cpp : 定義控制臺應用程序的入口點。
#include "stdafx.h"
#include <iostream>
using namespace std;
//定義隊列的節(jié)點
struct Node
{
int data;
Node* next;
};
//定義程序所需的特殊隊列
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;
}
}
//在隊列的尾部添加一個元素,節(jié)點不存在,需要程序創(chuàng)建
void push(int e)
{
Node* p = new Node;
p->data = e;
p->next = NULL;
rear->next = p;
rear = p;
}
//在隊列的尾部添加一個節(jié)點,節(jié)點原來就存在
void push(Node* p)
{
p->next = NULL;
rear->next = p;
rear = p;
}
//數(shù)據(jù)元素中最大位數(shù)
int lenData()
{
int temp(0);//數(shù)據(jù)元素的最大位數(shù)
int n(0); //單個數(shù)據(jù)元素具有的位數(shù)
int d; //用來存儲待比較的數(shù)據(jù)元素
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;
}
//判斷隊列是否為空
bool empty()
{
if (front == rear)
{
return true;
}
return false;
}
//清除隊列中的元素
void clear()
{
front->next = NULL;
rear = front;
}
//輸出隊列中的元素
void print(Queue& que)
{
Node* p = que.front->next;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
}
//基數(shù)排序
void RadixSort(Queue& que)
{
//定義一個指針數(shù)組,數(shù)組中存放十個分別指向十個隊列的指針
Queue* arr[10];
for (int i = 0; i < 10; i++)
{
arr[i] = new Queue;
}
int d = 1;
int m = que.lenData(); //取得待排序數(shù)據(jù)元素中的最大位數(shù)
//將初始隊列中的元素分配到十個隊列中
for(int i = 0; i < m; i++)
{
Node* p = que.front->next;
Node* q;
int k; //余數(shù)為k,則存儲在arr[k]指向的隊列中
while (p != NULL)
{
k = (p->data/d)%10;
q = p->next;
arr[k]->push(p);
p = q;
}
que.clear(); //清空原始隊列
//將十個隊列中的數(shù)據(jù)收集到原始隊列中
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++)//清空十個隊列
{
arr[i]->clear();
}
d *= 10;
}
print(que); //輸出隊列中排好序的元素
}
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;
}
下面的代碼轉自維基百科,還沒仔細分析,先拿過來
#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;
}
欄 目:C語言
下一篇:探討:用兩個棧實現(xiàn)一個隊列(我作為面試官的小結)
本文標題:C++實現(xiàn)基數(shù)排序的方法詳解
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/4389.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10數(shù)據(jù)結構課程設計-用棧實現(xiàn)表達式求值的方法詳解
- 01-10使用OpenGL實現(xiàn)3D立體顯示的程序代碼
- 01-10深入理解C++中常見的關鍵字含義
- 01-10求斐波那契(Fibonacci)數(shù)列通項的七種實現(xiàn)方法
- 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運算符做加法
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實現(xiàn)方法


閱讀排行
本欄相關
- 04-02c語言函數(shù)調用后清空內存 c語言調用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求
隨機閱讀
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實例總結
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什