起泡排序

#include<iostream>  
#include<vector>  
using namespace std;  
  
class BubbleSort  
{  
public:  
    void Input();   //数据输入  
    void Insert();  //直接插入排序  
    void Output();//数据输出  
private:  
    vector<int> vec;//vec[0]作为监视哨  
};  
  
void BubbleSort::Input()  
{  
    int a;  
    cout<<"第一个输入的数据元素作为监哨值,不作为表中的排序元素:"<<endl;  
    while(cin>>a)  
    {  
        vec.push_back(a);  
    }//while  
}//Input  
  
void BubbleSort::Insert()  
{  
    int i,j;  
	
	for(i=vec.size()-1;i>1;i--)
	{
		for(j=1;j<i;j++)
		{
			if(vec[j]>vec[j+1])//两个记录交换
			{
				vec[0]=vec[j+1];
				vec[j+1]=vec[j];
				vec[j]=vec[0];
			}//if
		}//for_j
	}//for_i
}//Insert  
  
void BubbleSort::Output()  
{  
    vector<int>::iterator it=vec.begin()+1;  
    while(it!=vec.end())  
    {  
        cout<<*it<<" ";  
        it++;  
    }//while  
}//Output  
  
void main()  
{  
    BubbleSort BS;  
    BS.Input();  
    BS.Insert();  
    BS.Output();  
}//main  
起泡排序_第1张图片

你可能感兴趣的:(起泡排序)