输入说明 :
第一行:顺序表A的数据元素的数据类型标记(0:int,1:double,2:char,3:string)
第二行:待排序顺序表A的数据元素(数据元素之间以空格分隔)
第三行:希尔排序的趟数
第四行:希尔排序每趟的步长(步长之间以空格分隔)
输出说明 :
如第一行输入值为0、1、2、3之外的值,直接输出“err”
否则:
第一行:待排序顺序表的遍历结果
第二行:第一趟的步长
第三行:第一趟的排序结果
...
第n行:最终的排序结果
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
//希尔排序(一趟)
template<class ElemType>
void ShellInsert(vector<ElemType> &a,int dk) {
int i,j;
ElemType temp;
cout<<dk<<endl;
for(i=dk; i<a.size(); i++) { //分别向每组的有序区域插入
temp=a[i];
for(j=i-dk; (j>=i%dk)&&a[j]>temp; j-=dk)
a[j+dk]=a[j];
if(j!=i-dk)
a[j+dk]=temp;
}
for( int m=0; m<a.size(); ++m)//
{
cout<<a[m];
if(m!=a.size()-1)cout<<' ';
else cout<<endl;
}
}
/*
0
49 38 65 97 76 13 27 49 55 4
3
5 3 1
*/
template<class ElemType>
//希尔排序(主函数)
void ShellSort( vector<ElemType> &a, int d[], int t ) { //t为希尔排序的趟数
for(int i=0; i<t; ++i) {
ShellInsert(a,d[i]);
}
}
//0:int,1:double,2:char,3:string
int main() {
int tmy,num,time,len[10];
string line,elem;
stringstream ss;
cin>>tmy;
getchar();
if(tmy!=0&&tmy!=1&&tmy!=2&&tmy!=3)
cout<<"err"<<endl;
else {
getline(cin,line);
ss<<line;
vector<string>v;
vector<int>vi;
vector<double>vd;
while(ss>>elem) {
if(tmy==0) {
int i=atoi(elem.c_str());
vi.push_back(i);
}
else if(tmy==1) {
double d=atof(elem.c_str());
vd.push_back(d);
} else
v.push_back(elem);
}
cin>>time;
for(int i=0; i<time; ++i)
cin>>len[i];
if(tmy==0)
ShellSort(vi,len,time);
else if(tmy==1)
ShellSort(vd,len,time);
else
ShellSort(v,len,time);
}
return 0;
}