zoukankan      html  css  js  c++  java
  • 冒泡排序和选择排序有什么区别?

    #include<iostream>
    using namespace std;
    //**二维数组排序改错,程序中存在非语法错误导致排序不成功,请修改
    #define M 2
    #define N 2
    class Agx
    {
    private:
        int a[M][N];
        int sort;//1代表利用选择排序升序 其他数字代表利用冒泡排序降序
    public:
        void setAgx()
        {
            int i,j;
            for(i=0;i<M;i++)
                for(j=0;j<N;j++)
                    cin>>a[i][j];
        }
        void sortAgx(int check=0)
        {
            sort = check;
            if(sort == 1)
                sortUp(a[0]);
            else
                sortDown(a[0]);
        }
        void sortUp(int *p)            //实际上可以把这个当做一维数组来想,*P传进去的就是指针 其实就是一串连续的地址,其实就是一维数组 
        {
            int i,j,temp;
            for(i=0;i<M*N-1;i++)
                for(j=i;j<M*N-1;j++)       //j=i 就是选择排序(J等于变量就是选择排序
                {
                    if(p[i]>p[j+1])
                    {
                        temp = p[i];
                        p[i] = p[j+1];
                        p[j+1] = temp;
                    }
                }
        }
        void sortDown(int *p)
        {
            int i,j,temp;
            for(i=0;i<M*N-1;i++)
            {
                for(j=0;j<M*N-1-i;j++)    //j=0就是冒泡排序
                {
                    if(p[j]<p[j+1])
                    {
                        temp = p[j];
                        p[j] = p[j+1];
                        p[j+1] = temp;
                    }
                }
            }
        }
        void showAgx()
        {
            int i,j;
            for(i=0;i<M;i++)
            {
                for(j=0;j<N;j++)
                    cout<<a[i][j]<<" ";
                cout<<endl;
            }
        }
    };
    int main()
    {
        Agx test;
        test.setAgx();
        test.sortAgx(1);
        test.showAgx();
        cout<<endl;
        test.sortAgx(0);
        test.showAgx();
    }
  • 相关阅读:
    Redis 连接命令
    Redis 脚本
    Redis 事务
    Redis 发布订阅
    Redis HyperLogLog
    Redis 有序集合(sorted set)
    Redis 集合(Set)
    Redis 列表(List)
    Redis 哈希(Hash)
    特定消费者的限制流量
  • 原文地址:https://www.cnblogs.com/zhangqianxi/p/14090288.html
Copyright © 2011-2022 走看看