zoukankan      html  css  js  c++  java
  • 冒泡排序

    冒泡排序

    冒泡排序就是将数据两两比较,将大的或者晓得往后排,排序一次得到一个最大值或者最小值

    代码主要把握比较趟数和两两比较次数

    for主要作用是控制循环次数让代码执行下去

    #include <iostream>
    using namespace std;
    template<typename T> 
    void  bubble_sort    (T * array, int len,bool ascending)
    {
    
            T tmp = 0;
            if(ascending)
            {
                for(int i = 0; i < len-1; i ++)
                {
                    for(int j = 0; j < len-i-1; j++)//留有余量
                    {
                        if(array[j]<=array[j+1])
                            continue;
                        else
                        {
                            tmp = array[j];
                            array[j] = array[j+1];
                            array[j+1] = tmp;
                        } 
                    }
            
                }
            }
            else
            {
                for(int i = 0; i < len-1; i ++)
                {
                    for(int j = 0; j < len-i-1; j++)
                    {
                        if(array[j]>=array[j+1])
                            continue;
                        else
                        {
                            tmp = array[j];
                            array[j] = array[j+1];
                            array[j+1] = tmp;
                        } 
                    }
            
                }
        
            }
    
    }
    
    
    int main()
    {
        float data[8];
        for(int i = 0; i<8;i++)
        cin>>data[i];
    
        cout<<"data"<<endl;
    
        for(int i = 0; i<8;i++)
        cout<<data[i]<<endl;
    
        bubble_sort<float>(data,8,false);
    
        cout<<"sorted data"<<endl;
        for(int i = 0; i<8;i++)
        cout<<data[i]<<endl;
        return 0;
    }
  • 相关阅读:
    团队冲刺第四天
    团队冲刺第三天
    团队冲刺第二天
    冲刺(六)
    冲刺(五)
    冲刺(四)
    冲刺(三)
    冲刺(二)
    冲刺(一 )
    第一阶段SCRUM
  • 原文地址:https://www.cnblogs.com/hong2016/p/6659118.html
Copyright © 2011-2022 走看看