c++课程设计,高手指点,谢谢.

来源:百度知道 编辑:UC知道 时间:2024/05/24 03:51:50
插入排序类模板的设计与实现
建立一维数组数据结构的模板类,使一维数组中的数据元素可以是char, int, float等多种数据类型,并对数组元素实现插入类排序。主要完成如下功能:
(1) 实现数组数据的输入和输出;
(2) 实现直接插入排序功能;
(3) 实现2-路插入排序功能;
(4) 实现希尔排序功能;
(5) 将每种排序功能作为类的成员函数实现,编写主函数测试上述排序功能。

// sort type: insert sort
// complexity: O(n^2)
// Algorithm: insert the n+1 element to an already sorted n element array
template <typename T>
struct InsertionSorter
{
void Sort(T list[], int n)
{
for(int i = 1; i <= n-1; ++i)
{
int nInsert = list[i];
int j = i - 1;
while(j >= 0 && list[j] > nInsert)
{
list[j+1] = list[j];
--j;
}
list[j+1] = nInsert;
}
}
};