数据结构课程设计个人任务4:冒泡排序

输入说明 :

第一行:顺序表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>

void BubbleSort( vector<ElemType> &a ) {
    ElemType tmp;
    bool exchange=0;
    for(int i=a.size()-1; i>=0; --i){//后面为有序区

        exchange=0;
        for(int j=0; j<i; ++j) {//无序区里面进行交换

            if(a[j+1]<a[j]) {
                tmp=a[j];
                a[j]=a[j+1];
                a[j+1]=tmp;
                exchange=1;
            }


        }if(exchange==0)
                break;
        print(a);
    }

}

//0:int,1:double,2:char,3:string

int main() {
    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)
        BubbleSort(vi);
    else if(tmy==1)
        BubbleSort(vd);
    else
        BubbleSort(v);
        return 0;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值