c++怎么面向对象思想实现冒泡排序?

c++怎么面向对象思想实现冒泡排序,要求既可以从小到大排序,又可以从大到小排序。


#include  
using namespace std;
class sort //冒泡排序类,只需排序功能
{
public:
    void sortarray(int a[],int size);
};

//////////////////////////////////////////////////////
void main()
{
 const int Maxsize=5;  //数组元素个数可以改变,暂设为5
 int arr[Maxsize],i;
 cout<<"请输入Maxsize个数:"<>arr[i];

         sort s1; //构造排序类的对象
 s1.sortarray(arr,Maxsize); //进行排序

 cout<<"排序结果:"<a[j-1])  
     {
        temp = a[j]; a[j] = a[j-1]; a[j-1] = temp;
     }
  }
 }
}

面向对象,就建一个类哦,有帮助的话采纳一下哦,谢谢!

用面向对象思想实现代码如下:

#include <iostream>
using namespace std;

class Bubble
{
private:
    int *a;
    int len=0;

public:
    //构造函数
    Bubble(int *b,int n)
    {
        a=b;
        len = n;
    }
    void sort(bool f)
    {
        int i,j,t;
        for (i=0;i<len-1;i++)
        {
            for (j=0;j<len-1-i;j++)
            {
                if ( (f && a[j]>a[j+1]) || ((!f) && a[j]<a[j+1]))
                {
                    t = a[j]; a[j]=a[j+1]; a[j+1]=t;
                }
            }
        }
    }
    void show()
    {
        int i;
        for (i=0;i<len;i++)
            cout << a[i]<<" ";
        cout << endl;
    }
};


int main()
{
    int i,n;
    cout << "输入个数n:";
    cin >> n;
    int a[n];
    cout << "输入"<< n <<"个数:";
    for (i=0;i<n;i++)
        cin >> a[i];
    Bubble p(a,n);
    cout << "从小到大排序"<<endl;
    p.sort(true);
    p.show();
    cout << "从大到小排序"<<endl;
    p.sort(false);
    p.show();
   return 0;
}

img

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

运行结果如下:

img

代码如下:

#include <iostream>
using namespace std;
//n表示数组的长度,flag=1表示从小到大,0表示从大到小
void bubble_sort(double a[],int n,int flag )
{
    int i,j;
    double t;
    for (i=0;i<n-1;i++)
    {
        for (j=0;j<n-1-i;j++)
        {
            if ( (flag && a[j]>a[j+1]) || ( (flag==0)&& a[j]<a[j+1] ))
            {
                t = a[j];
                a[j]=a[j+1];
                a[j+1]=t;
            }
        }
    }
}

int main()
{
    double a[10];
    int i,j;
    cout << "请输入10个数:";
    for (i=0;i<10;i++)
    {
        cin >> a[i];
    }
    int flag = 0;
    cout << "0:从大到小排序"<<endl;
    cout << "1: 从小到大排序"<<endl;
    cout << "请输入你的选择:";
    cin >> flag;
    //排序
    bubble_sort(a,10,flag);
    //输出
    cout << "排序后:"<<endl;
    for (i=0;i<10;i++)
    {
        cout << a[i]<<" ";
    }
    return 0;
}

从小到大排序


/* Note:Your choice is C IDE */
#include "stdio.h"

//冒泡排序,从小到大
void choose(int a[10]){
    int i,j,temp;
    for(i=1;i<10;i++)
    {
        for(j=9;j>=i;j--)
        {
            if(a[j]<a[j-1]){   //把小于好改为大于号,就是从大到小排序
                temp=a[j];
                a[j]=a[j-1];
                a[j-1]=temp;
            }
        }    
    }    
}
//输出
void output(int a[10]){
    int i;
    for(i=0;i<10;i++)
        printf("%d\t",a[i]);    
}
void main()
{
    
    int b[20];
    int a[10];
    int i,cnt=0;
    for(i=0;i<20;i++){
        scanf("%d",&b[i]);
        if(i%2==0){
            a[cnt++]=b[i];
        }    
    }
    //冒泡排序
    choose(a);
    //输出
    output(a);
}