二分插入排序

来源:互联网 发布:查看linux下所有用户 编辑:程序博客网 时间:2024/05/18 01:02

今天,我们一起用C++实现二分插入排序,具体如下。

Data.h具体内容:

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

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

[cpp] view plaincopy
  1. #include "Data.h"  
  2. #include "LinkQueue.h"  
  3.   
  4. template<typename Type> class Sort{  
  5. public:  
  6.     void BinaryInsertSort(DataList<Type> &list, int n = -1);  
  7. };  
  8. template<typename Type>  
  9. void Sort<Type>::BinaryInsertSort(DataList<Type> &list, int n){  
  10.     if (-1 == n){  
  11.         for (int i = 1; i < list.m_ncurrentsize; i++){  
  12.             BinaryInsertSort(list, i);  
  13.         }  
  14.         return;  
  15.     }  
  16.     Element<Type> temp = list.m_pvector[n];  
  17.     int left = 0, right = n - 1;  
  18.     while (left <= right){  
  19.         int middle = (left + right) / 2;  
  20.         if (temp < list.m_pvector[middle]){  
  21.             right = middle - 1;  
  22.         }  
  23.         else {  
  24.             left = middle + 1;  
  25.         }  
  26.     }  
  27.     for (int i = n - 1; i >= left; i--){  
  28.         list.m_pvector[i + 1] = list.m_pvector[i];  
  29.     }  
  30.     list.m_pvector[left] = temp;  
  31. }  
main.cpp具体内容如下:

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

图1 运行效果

0 0
原创粉丝点击