排序算法(2)-冒泡排序

来源:互联网 发布:养殖场记账软件 编辑:程序博客网 时间:2024/05/17 22:46
#include <iostream>

using namespace std;

class bubble_sort
{
public:
bubble_sort(int *a,int len)
{
this->a = a;
this->num = len;
}


void sort()
{
for(int i = 0;i<num;i++)
{
bubble_exchge(a,(num-i-1));
}
}


void display()
{
for(int i = 0;i<num;i++)
cout<<i<<"="<<a[i]<<endl;
}


protected:
void bubble_exchge(int *a,int end)
{
for(int i = 0;i<end;i++)
if(a[i]>a[i+1])
{
swap(a[i],a[i+1]);
}      

}

void swap(int &a,int &b)
{
int tmp = b;
b = a;
a = tmp;
}

private:
int num;
int *a;
};




int main(void)
{

int s[12]={1,6,2,3,10,9,7,8,5,34,56,4};

bubble_sort sorter(s,12);
sorter.sort();
sorter.display();


return 0;
}