设为首页 加入收藏

TOP

排序算法模板实现(一)
2019-03-24 12:08:06 】 浏览:154
Tags:排序 算法 模板 实现

常见的排序算法模板实现

学过的东西总是很容易忘记,最近用模板整理 了常见的排序算法。涉及模板的知识在这里我就不细细的介绍了。

#pragma once
?
#define NULL 0
#define MARK -65535
?
?
template<class T> //声明
class CDaXiongTemplateSort
{
public:
CDaXiongTemplateSort();
~CDaXiongTemplateSort();
void SetMember(T*, int ); //初始化数据成员
T*   BubbleSort(bool ascending = true); //冒泡排序 默认升序
T* SelectionSort(bool ascending = true); //选择排序
T* InsertionSort(bool ascending = true); //插入排序
T* ShellSort(bool ascending = true); //希尔排序
T*   MergeSort(T*, int size, bool ascending = true);    //归并排序 start 为起始标号,end 为结束标号
T*   BucketSort(int n); //桶排序   n表示数的位数,只适用于正整数
?
?
?
protected:
T *m_num;
int m_length;
};
?
template<class T>
CDaXiongTemplateSort<T>::CDaXiongTemplateSort()
{
this->m_num = nullptr;
}
?
template<class T>
CDaXiongTemplateSort<T>::~CDaXiongTemplateSort()
{
if (nullptr != this->m_num)
{
delete[]this->m_num;
}
this->m_num = nullptr;
}
?
template<class T>
void CDaXiongTemplateSort<T>::SetMember(T* num, int length)
{
this->m_num = new T[length];
this->m_length = length;
for (int i = 0; i < length; ++i)
{
this->m_num[i] = num[i];
}
}
?
?
//冒泡
template<class T>
T* CDaXiongTemplateSort<T>::BubbleSort(bool ascending = true)
{

T tempNum;
for (int i = 0; i < this->m_length; ++i)
{
for (int j = 0; j < this->m_length - 1 - i; ++j)
{
if (ascending)
{
if (this->m_num[j] > this->m_num[j + 1])
{
tempNum = this->m_num[j];
this->m_num[j] = this->m_num[j + 1];
this->m_num[j + 1] = tempNum;
}

}
else
{
if (this->m_num[j] < this->m_num[j + 1])
{
tempNum = this->m_num[j];
this->m_num[j] = this->m_num[j + 1];
this->m_num[j + 1] = tempNum;
}

}

}

}

return this->m_num;
}
?
?
//选择
template<class T>
T* CDaXiongTemplateSort<T>::SelectionSort(bool ascending = true)
{
T tempNum;
for (int i = 0; i < this->m_length - 1; ++i)
{
for (int j = i + 1; j < this->m_length; ++j)
{
if (ascending)
{
if (this->m_num[i] > this->m_num[j])
{
tempNum = this->m_num[j];
this->m_num[j] = this->m_num[i];
this->m_num[i] = tempNum;
}

}
else
{
if (this->m_num[i] < this->m_num[j])
{
tempNum = this->m_num[j];
this->m_num[j] = this->m_num[i];
this->m_num[i] = tempNum;
}

}
?
}
?
}
?
return this->m_num;
}
?
//插入
template<class T>
T* CDaXiongTemplateSort<T>::InsertionSort(bool ascending = true)
{
T tempNum;
int mark;
for (int i = 0; i < this->m_length - 1; ++i)
{
tempNum = this->m_num[i + 1];
for (int j = i + 1; j > 0 ; --j)
{
if (ascending)
{
if (tempNum < this->m_num[j - 1])
{
this->m_num[j] = this->m_num[j - 1];
this->m_num[j - 1] = tempNum;
}

}
else
{
if (tempNum > this->m_num[j - 1])
{
this->m_num[j] = this->m_num[j - 1];
this->m_num[j - 1] = tempNum;
}

}


}
}
return this->m_num;
}
?
?
//希尔
template<class T>
T* CDaXiongTemplateSort<T>::ShellSort(bool ascending = true)
{
T tempNum;
int step = this->m_length / 2;
while (step > 0)
{
for (int i = 0; i < this->m_length - step; ++i)
{
tempNum = this->m_num[i + step];
for (int j = i + step; j > 0 ; j-=step)
{
if (ascending)
{
if ((j - step) >= 0 && (tempNum < this->m_num[j - step]))//此处若用for循环则要判别 j - srep < 0的情况
{
this->m_num[j] = this->m_num[j - step];
this->m_num[j - step] = tempNum;
}
}
else
{
if ((j - step) >= 0 && (tempNum > this->m_num[j - step]))//此处若用for
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇51nod“省选”模测第二场 B 异或.. 下一篇#leetcode刷题之路32-最长有效括号

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目