基數排序

尹成發表於2014-09-14

    今天,我們一起用C++實現基數排序,具體程式碼如下:

Data.h具體內容如下:

template<typename Type> class Element{
public:
	Type GetKey(){
		return key;
	}

	void SetKey(Type item){
		key = item;
	}

public:
	Element<Type>& operator =(Element<Type> copy){
		key = copy.key;
		return *this;
	}

	bool operator ==(Element<Type> item){
		return this->key == item.key;
	}

	bool operator !=(Element<Type> item){
		return this->key != item.key;
	}

	bool operator <(Element<Type> item){
		return this->key < item.key;
	}

	bool operator >(Element<Type> item){
		return this->key > item.key;
	}

	bool operator >=(Element<Type> item){
		return this->key >= item.key;
	}

	bool operator <=(Element<Type> item){
		return this->key <= item.key;
	}


private:
	Type key;
};

template<typename Type> class Sort;
template<typename Type> class DataList{
public:
	friend class Sort < Type > ;
	DataList(int size = m_nDefaultSize) : m_nMaxSize(size), m_ncurrentsize(0){
		m_pvector = new Element<Type>[size];
	}

	DataList(Type *data, int size);

	bool Insert(Type item);
	~DataList(){
		delete[] m_pvector;
	}

	int Size(){
		return this->m_ncurrentsize;
	}
	void Swap(Element<Type> &left, Element<Type> &right){
		Element<Type> temp = left;
		left = right;
		right = temp;
	}

	void Print();
private:
	static const int m_nDefaultSize = 10;
	Element<Type> *m_pvector;
	const int m_nMaxSize;
	int m_ncurrentsize;
};

template<typename Type> DataList<Type>::DataList(Type *data, int size)
	: m_nMaxSize(size > m_nDefaultSize ? size : m_nDefaultSize), m_ncurrentsize(0){
	this->m_pvector = new Element<Type>[size];
	for (int i = 0; i < size; i++){
		this->m_pvector[i].SetKey(data[i]);
	}
	this->m_ncurrentsize += size;

}

template<typename Type> bool DataList<Type>::Insert(Type item){
	if (this->m_ncurrentsize == this->m_nMaxSize){
		cerr << "The list is full!" << endl;
		return 0;
	}
	this->m_pvector[this->m_ncurrentsize++].SetKey(item);
}

template<typename Type> void DataList<Type>::Print(){
	cout << "The list is:";
	for (int i = 0; i < this->m_ncurrentsize; i++){
		cout << " " << this->m_pvector[i].GetKey();
	}
}
LinkQueue.h具體內容如下:

#include "QueueNode.h"

template<typename Type> class LinkQueue{
public:
	LinkQueue() :m_prear(NULL), m_pfront(NULL){}
	~LinkQueue(){
		MakeEmpty();
	}
	void Append(const Type item);
	Type Delete();
	Type GetFront();
	void MakeEmpty();
	bool IsEmpty() const{
		return m_pfront == NULL;
	}
	void Print();

private:
	QueueNode<Type> *m_prear, *m_pfront;
};

template<typename Type> void LinkQueue<Type>::MakeEmpty(){
	QueueNode<Type> *pdel;
	while (m_pfront){
		pdel = m_pfront;
		m_pfront = m_pfront->m_pnext;
		delete pdel;
	}
}

template<typename Type> void LinkQueue<Type>::Append(const Type item){
	if (m_pfront == NULL){
		m_pfront = m_prear = new QueueNode<Type>(item);
	}
	else{
		m_prear = m_prear->m_pnext = new QueueNode<Type>(item);
	}
}

template<typename Type> Type LinkQueue<Type>::Delete(){
	if (IsEmpty()){
		cout << "There is no element!" << endl;
		exit(1);
	}
	QueueNode<Type> *pdel = m_pfront;
	Type temp = m_pfront->m_data;
	m_pfront = m_pfront->m_pnext;
	delete pdel;
	return temp;
}

template<typename Type> Type LinkQueue<Type>::GetFront(){
	if (IsEmpty()){
		cout << "There is no element!" << endl;
		exit(1);
	}
	return m_pfront->m_data;
}

template<typename Type> void LinkQueue<Type>::Print(){
	QueueNode<Type> *pmove = m_pfront;
	cout << "front";
	while (pmove){
		cout << "--->" << pmove->m_data;
		pmove = pmove->m_pnext;
	}
	cout << "--->rear" << endl << endl << endl;
}
QueueNode.h具體內容如下:

template<typename Type> class LinkQueue;

template<typename Type>
class QueueNode
{
private:
	friend class LinkQueue < Type > ;
	QueueNode(const Type item, QueueNode<Type> *next = NULL)
		:m_data(item), m_pnext(next){}
private:
	Type m_data;
	QueueNode<Type> *m_pnext;
};
Sort.h具體內容如下:

#include "Data.h"
#include "LinkQueue.h"

template<typename Type> class Sort{
public:
    void RadixSort(DataList<int> &list, int m, int d);      //just use for integer!
};
template<typename Type>
void Sort<Type>::RadixSort(DataList<int> &list, int m, int d)
{
	LinkQueue<int> *queue = new LinkQueue<int>[d];
	int power = 1;
	for (int i = 0; i < m; i++){
		if (i){
			power = power * d;
		}
		for (int j = 0; j < list.m_ncurrentsize; j++){
			int k = (list.m_pvector[j].GetKey() / power) % d;
			queue[k].Append(list.m_pvector[j].GetKey());
		}

		for (int j = 0, k = 0; j < d; j++){
			while (!queue[j].IsEmpty()){
				list.m_pvector[k++].SetKey(queue[j].Delete());
			}
		}
	}
}
main.cpp具體內容如下:

#include <iostream>
using namespace std;
#include "Sort.h"
int main()
{
	int init[15] = { 1, 3, 5, 7, 4, 2, 8, 0, 6, 9, 29, 13, 25, 11, 32 };
	DataList<int> data(init, 15);
	Sort<int> sort;
	data.Print();
	cout << endl << endl << endl;
	sort.RadixSort(data, 2, 10);
	data.Print();
	cin.get();
	return 0;
}
執行效果如圖1所示:

圖1 執行效果

相關文章