希尔排序
来源:互联网 发布:ajax返回json为null 编辑:程序博客网 时间:2024/06/04 18:20
今天,我们一起用C++实现希尔排序,具体的代码如下:
Data.h内容如下:
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 InsertSort(DataList<Type> &list, int n = -1);
- void ShellSort(DataList<Type> &list, const int gap = -1);
- };
-
- template<typename Type>
- void Sort<Type>::InsertSort(DataList<Type> &list, int n){
- if (-1 == n){
- for (int i = 1; i < list.m_ncurrentsize; i++){
- InsertSort(list, i);
- }
- return;
- }
- Element<Type> temp = list.m_pvector[n];
- int i;
- for (i = n; i>0; i--){
- if (temp > list.m_pvector[i - 1]){
-
- break;
- }
- else{
- list.m_pvector[i] = list.m_pvector[i - 1];
- }
- }
- list.m_pvector[i] = temp;
- }
-
- template<typename Type>
- void Sort<Type>::ShellSort(DataList<Type> &list, const int gap){
- if (-1 == gap){
- int gap = list.m_ncurrentsize / 2;
- while (gap){
- ShellSort(list, gap);
- gap = (int)(gap / 2);
- }
- return;
- }
- for (int i = gap; i < list.m_ncurrentsize; i++){
- InsertSort(list, i);
- }
- }
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.ShellSort(data);
- data.Print();
- cin.get();
- return 0;
- }
运行效果如图1所示:图1 运行效果
0 0