输入说明 :
第一行:顺序表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 print(vector<ElemType> a) {
for( int m=0; m<a.size(); ++m) { //
cout<<a[m];
if(m!=a.size()-1)
cout<<',';
else
cout<<endl;
}
}
//划分函数(快速排序)
template<class ElemType>
int divide( vector<ElemType> &a, int low, int high )
{
ElemType tmp=a[low];
while(low<high)
{
while(low<high&&tmp<a[high])
high--;
a[low]=a[high];
while(low<high&&tmp>a[low])
low++;
a[high]=a[low];
}
if(low==high)
{
a[low]=tmp;
}
print(a);
return low;//记录枢轴位置
}
//快速排序的实现(递归)
template<class ElemType>
void QuickSort( vector<ElemType> &a, int low, int high)
{
int Q;
if(low<high)
{
Q=divide(a,low,high);
QuickSort(a,low,Q-1);
QuickSort(a,Q+1,high);
}
}
template<class ElemType>
void QuickSort( vector<ElemType> &a)
{
if(a.size()==1){cout<<a[0]<<endl;return;}
QuickSort(a,0,a.size()-1);
}
int main() {//0:int,1:double,2:char,3:string
int tmy;
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);
}
if(tmy==0)
QuickSort(vi);
else if(tmy==1)
QuickSort(vd);
else
QuickSort(v);
return 0;
}
}