排序:交换排序——冒泡排序法

Bubblesort.h
#ifndef BUBBLESORT_H
#define BUBBLESORT_H

#include 
using namespace std;

class BubbleSort
{
private:
    int len;
    vector list;
public:
    BubbleSort(vector _list, int _len);
    void bubblesort();
    void swap(int , int );
    void out();
};

#endif
Bubblesort.cpp
#include "BubbleSort.h"
#include 
using namespace std;
    
BubbleSort::BubbleSort(vector _list, int _len)
{
    for(int i=0; i<_len; i++)
	list.push_back(_list[i]);
    this->len = _len;
}

void BubbleSort::bubblesort()
{
    for(int i=0; i list[j]) swap(i, j);
}

void BubbleSort::swap(int i, int j)
{
    int temp = list[i];
    list[i] = list[j];
    list[j] = temp;
}

void BubbleSort::out()
{
    for(int i=0; i


main.cpp主函数

#include "BubbleSort.h"
#include
#include 

int main()
{
    vector list;
    int tmp;
    cout << "please input num[]: ";
    while(cin >> tmp && tmp != 0){
        list.push_back(tmp);
    }
    BubbleSort bub(list, list.size());
    bub.bubblesort();
    bub.out();
    return 0;
    
}

你可能感兴趣的:(C/C++,算法,数据结构,STL)