选择排序

   今天,我们一起用C++实现一个选择排序,帮助大家熟练C++的语法,具体内容如下:

Data.h的内容:

[cpp]  view plain copy
  1. template<typename Type>   
  2. class Element{  
  3. public:  
  4.     Type GetKey(){  
  5.         return key;  
  6.     }  
  7.   
  8.     void SetKey(Type item){  
  9.         key = item;  
  10.     }  
  11.   
  12. public:  
  13.     Element<Type>& operator =(Element<Type> copy){  
  14.         key = copy.key;  
  15.         return *this;  
  16.     }  
  17.   
  18.     bool operator ==(Element<Type> item){  
  19.         return this->key == item.key;  
  20.     }  
  21.   
  22.     bool operator !=(Element<Type> item){  
  23.         return this->key != item.key;  
  24.     }  
  25.   
  26.     bool operator <(Element<Type> item){  
  27.         return this->key < item.key;  
  28.     }  
  29.   
  30.     bool operator >(Element<Type> item){  
  31.         return this->key > item.key;  
  32.     }  
  33.   
  34.     bool operator >=(Element<Type> item){  
  35.         return this->key >= item.key;  
  36.     }  
  37.   
  38.     bool operator <=(Element<Type> item){  
  39.         return this->key <= item.key;  
  40.     }  
  41.   
  42.   
  43. private:  
  44.     Type key;  
  45. };  
  46.   
  47. template<typename Type> class Sort;  
  48. template<typename Type> class DataList{  
  49. public:  
  50.     friend class Sort < Type > ;  
  51.     DataList(int size = m_nDefaultSize) : m_nMaxSize(size), m_ncurrentsize(0){  
  52.         m_pvector = new Element<Type>[size];  
  53.     }  
  54.   
  55.     DataList(Type *data, int size);  
  56.   
  57.     bool Insert(Type item);  
  58.     ~DataList(){  
  59.         delete[] m_pvector;  
  60.     }  
  61.   
  62.     int Size(){  
  63.         return this->m_ncurrentsize;  
  64.     }  
  65.     void Swap(Element<Type> &left, Element<Type> &right){  
  66.         Element<Type> temp = left;  
  67.         left = right;  
  68.         right = temp;  
  69.     }  
  70.   
  71.     void Print();  
  72. private:  
  73.     static const int m_nDefaultSize = 10;  
  74.     Element<Type> *m_pvector;  
  75.     const int m_nMaxSize;  
  76.     int m_ncurrentsize;  
  77. };  
  78.   
  79. template<typename Type> DataList<Type>::DataList(Type *data, int size)  
  80.     : m_nMaxSize(size > m_nDefaultSize ? size : m_nDefaultSize), m_ncurrentsize(0){  
  81.     this->m_pvector = new Element<Type>[size];  
  82.     for (int i = 0; i < size; i++){  
  83.         this->m_pvector[i].SetKey(data[i]);  
  84.     }  
  85.     this->m_ncurrentsize += size;  
  86.   
  87. }  
  88.   
  89. template<typename Type> bool DataList<Type>::Insert(Type item){  
  90.     if (this->m_ncurrentsize == this->m_nMaxSize){  
  91.         cerr << "The list is full!" << endl;  
  92.         return 0;  
  93.     }  
  94.     this->m_pvector[this->m_ncurrentsize++].SetKey(item);  
  95. }  
  96.   
  97. template<typename Type> void DataList<Type>::Print(){  
  98.     cout << "The list is:";  
  99.     for (int i = 0; i < this->m_ncurrentsize; i++){  
  100.         cout << " " << this->m_pvector[i].GetKey();  
  101.     }  
  102. }  
LinkQueue.h具体内容如下:

[cpp]  view plain copy
  1. #include "QueueNode.h"  
  2.   
  3. template<typename Type> class LinkQueue{  
  4. public:  
  5.     LinkQueue() :m_prear(NULL), m_pfront(NULL){}  
  6.     ~LinkQueue(){  
  7.         MakeEmpty();  
  8.     }  
  9.     void Append(const Type item);  
  10.     Type Delete();  
  11.     Type GetFront();  
  12.     void MakeEmpty();  
  13.     bool IsEmpty() const{  
  14.         return m_pfront == NULL;  
  15.     }  
  16.     void Print();  
  17.   
  18. private:  
  19.     QueueNode<Type> *m_prear, *m_pfront;  
  20. };  
  21.   
  22. template<typename Type> void LinkQueue<Type>::MakeEmpty(){  
  23.     QueueNode<Type> *pdel;  
  24.     while (m_pfront){  
  25.         pdel = m_pfront;  
  26.         m_pfront = m_pfront->m_pnext;  
  27.         delete pdel;  
  28.     }  
  29. }  
  30.   
  31. template<typename Type> void LinkQueue<Type>::Append(const Type item){  
  32.     if (m_pfront == NULL){  
  33.         m_pfront = m_prear = new QueueNode<Type>(item);  
  34.     }  
  35.     else{  
  36.         m_prear = m_prear->m_pnext = new QueueNode<Type>(item);  
  37.     }  
  38. }  
  39.   
  40. template<typename Type> Type LinkQueue<Type>::Delete(){  
  41.     if (IsEmpty()){  
  42.         cout << "There is no element!" << endl;  
  43.         exit(1);  
  44.     }  
  45.     QueueNode<Type> *pdel = m_pfront;  
  46.     Type temp = m_pfront->m_data;  
  47.     m_pfront = m_pfront->m_pnext;  
  48.     delete pdel;  
  49.     return temp;  
  50. }  
  51.   
  52. template<typename Type> Type LinkQueue<Type>::GetFront(){  
  53.     if (IsEmpty()){  
  54.         cout << "There is no element!" << endl;  
  55.         exit(1);  
  56.     }  
  57.     return m_pfront->m_data;  
  58. }  
  59.   
  60. template<typename Type> void LinkQueue<Type>::Print(){  
  61.     QueueNode<Type> *pmove = m_pfront;  
  62.     cout << "front";  
  63.     while (pmove){  
  64.         cout << "--->" << pmove->m_data;  
  65.         pmove = pmove->m_pnext;  
  66.     }  
  67.     cout << "--->rear" << endl << endl << endl;  
  68. }  
QueueNode.h具体内容如下:

[cpp]  view plain copy
  1. template<typename Type> class LinkQueue;  
  2. template<typename Type>  
  3. class QueueNode  
  4. {  
  5. private:  
  6.     friend class LinkQueue < Type > ;  
  7.     QueueNode(const Type item, QueueNode<Type> *next = NULL)  
  8.         :m_data(item), m_pnext(next){}  
  9. private:  
  10.     Type m_data;  
  11.     QueueNode<Type> *m_pnext;  
  12. };  
Sort.h具体内容如下:

[cpp]  view plain copy
  1. #include "Data.h"  
  2. #include "LinkQueue.h"  
  3. template<typename Type> class Sort{  
  4. public:  
  5.     void SelectSort(DataList<Type> &list);  
  6. private:  
  7.     void SelectChange(DataList<Type> &list, const int n);   
  8. };  
  9. template<typename Type>  
  10. void Sort<Type>::SelectChange(DataList<Type> &list, const int n){  
  11.     int j = n;  
  12.     for (int i = n + 1; i < list.m_ncurrentsize; i++){  
  13.         if (list.m_pvector[i] < list.m_pvector[j]){  
  14.             j = i;  
  15.         }  
  16.     }  
  17.     if (j != n){  
  18.         list.Swap(list.m_pvector[n], list.m_pvector[j]);  
  19.     }  
  20. }  
  21. template<typename Type>  
  22. void Sort<Type>::SelectSort(DataList<Type> &list){  
  23.     for (int i = 0; i < list.m_ncurrentsize - 1; i++){  
  24.         SelectChange(list, i);  
  25.     }  
  26. }  
test.cpp具体内容如下:

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

图1 运行效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值