设计一维数组的排序类Sort

smile_Jacker 2014-05-17 04:39:26
题目一:设计一维数组的排序类Sort
设计要求:
1. 程序结构清晰,代码可读性强;
2. 具备“直接插入排序”、“折半插入排序”、“冒泡排序”三种排序方法;
3. 能够对int/float/double/char 等数据类型进行排序;
4. 运用虚函数、类模板等技术手段。
5. 统计完成排序所花费的时间。
刚学C++,求大神帮忙指教指教!
...全文
137 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
smile_Jacker 2014-05-17
  • 打赏
  • 举报
回复
#include <stdlib.h> #include <iostream.h> #include <stdio.h> template< typename T > //类模版 /***************************定义一个基类Sort和三个派生类DirectInsertion,BinaryInsertion,Bubble*****************************/ //定义结构体 struct sqtable { T a[30]; T length; //一维数组的长度 }; //定义基类Sort template< typename T > //类模版 class Sort { protected: int i, j; public: //template <typename ElementType > //函数模版声明,数组a的类型为ElementType //定义构造函数 Sort(sqtable * st) { Sorts(st); } virtual void Sorts(sqtable * st) //排序函数定义为纯虚函数 { cout<<"这是排序函数"<<endl } void createst(sqtable *st) //创建一个长度为length的一维数组,a[0]=0,且不参与排序(创建数组函数) { printf("Please input data:"); st->a[0]=0; for (i=1;i<=st->length;i++) //i<=k { scanf("%d",&st->a[i]); } } void print(sqtable st) //输出函数,输出数组a[1]...a[length]的值 { printf("创建的一维数组为:"); for (i=1; i<=st.length; i++) { cout<<st.a[i]<<" "; } cout<<endl; } }; //定义派生类DInsertSort,直接插入排序 template <typename ElementType > class DInsertSort : public Sort { public: void DInsertSort(sqtable * st ) : Sort(sqtable * st){} //构造函数 void Sorts(sqtable * st ) //定义排序函数 { int i,j; for (i=2;i < st->length + 1;i++) { st->a[0]=st->a[i]; for (j=i-1;st->a[0] < st->a[j];j--) //a[1]...a[i-1]是有序数列 { st->a[j+1] = st->a[j]; } st->a[j+1] =st->a[0]; } } }; //定义派生类BInsertSort,折半插入排序 template< typename T > class BInsertSort : public Sort { private: protected: public: // BInsertSort ( sqtable * st ) //构造函数 void Sorts ( sqtable * st ) //折半插入排序 { int low,high,mid; for (i=2; i<=st->length; i++) { st->a[0]=st->a[i]; //将a[i]暂存到a[0] low=1; high=i-1; while (low<=high) //在有序数列 a[1]...a[i-1]中折半查找插入位置; { mid=(low+high)/2; //折半 if (st->a[0] < st->a[mid]) { high=mid-1; //插入点在左半部分 } else { low=mid+1; //插入点在右半部分 } } for (j=i-1; j>=high+1;j--) { st->a[j+1] = st->a[j]; //记录后移 } st->a[high+1] = st->a[0]; //记录后移 } } }; //定义派生类BubbleSort,冒泡排序 class BubbleSort : public Sort { private: protected: public: template <typename ElementType > //函数模版声明,数组a的类型为ElementType ElementType BubbleSort ( sqtable *st ) : Sort(sqtable * st){} //构造函数 ElementType Sorts ( sqtable *st ) //冒泡排序函数 { ElementType temp ; for ( i = st->length; i > 1; i -- ) { for ( j = 1; j < i; j ++ ) { if ( st->a[j] > st->a[j+1] ) { temp = st->a[j] ; st->a[j] = st->a[j+1] ; st->a[j+1] = temp ; //第一次循环结束以后最大的数会出现在最右边的位置 } } } } };
glsssd 2014-05-17
  • 打赏
  • 举报
回复
这种从头到尾问的拒绝回答,如果说自己写好了就把自己写的贴上来,然后哪里不会就针对那个地方问好了,别人是帮你纠错改正,不是所有代码全部帮你写
smile_Jacker 2014-05-17
  • 打赏
  • 举报
回复
我已经写好排序了,就是类模版不会!
FightForProgrammer 2014-05-17
  • 打赏
  • 举报
回复
你应该自己先写写,或者网上看看,哪里有问题在来问嘛.从头到尾问就没意义了

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧