zoukankan      html  css  js  c++  java
  • 47 选择排序和插入排序

    原文:https://www.cnblogs.com/wanmeishenghuo/p/9683786.html 参考狄泰软件相关教程

    添加选择排序:

    #ifndef SORT_H
    #define SORT_H
    
    #include "Object.h"
    
    namespace DTLib
    {
    
    class Sort : public Object
    {
    private:
        Sort();
        Sort(const Sort&);
        Sort& operator = (const Sort&);
    
        template <typename T>
        static void Swap(T& a, T& b)
        {
            T c(a);
            a = b;
            b = c;
        }
    
    public:
        template < typename T >
        static void Select(T array[], int len)
        {
            for(int i = 0; i < len; i++)
            {
                int min = i;
                for(int j = i + 1; j < len; j++)
                {
                    if( array[min] > array[j] )
                    {
                        min = j;
                    }
                }
    
                if( min != i)
                {
                    Swap(array[i], array[min]);
                }
            }
        }
    };
    
    }
    
    #endif // SORT_H
    

    测试程序如下:

    #include <iostream>
    #include <cstring>
    #include "DTString.h"
    #include "LinkList.h"
    #include "Object.h"
    #include "Sort.h"
    
    using namespace std;
    using namespace DTLib;
    
    
    int main()
    {
        int array[] = {3, 1, 2, 5, 4};
    
        Sort::Select(array, 5);
    
        for(int i=0; i < 5; i++)
        {
            cout << array[i] << endl;
        }
    
        return 0;
    }
    

    结果如下:

    给Select添加一个参数,支持从小到大排序和从大到小排序:

    #ifndef SORT_H
    #define SORT_H
    
    #include "Object.h"
    
    namespace DTLib
    {
    
    class Sort : public Object
    {
    private:
        Sort();
        Sort(const Sort&);
        Sort& operator = (const Sort&);
    
        template <typename T>
        static void Swap(T& a, T& b)
        {
            T c(a);
            a = b;
            b = c;
        }
    
    public:
        template < typename T >
        static void Select(T array[], int len, bool min2max=true)
        {
            for(int i = 0; i < len; i++)
            {
                int min = i;
                for(int j = i + 1; j < len; j++)
                {
                    if( min2max ? (array[min] > array[j]) : (array[min] < array[j]) )
                    {
                        min = j;
                    }
                }
    
                if( min != i)
                {
                    Swap(array[i], array[min]);
                }
            }
        }
    };
    
    }
    
    #endif // SORT_H
    

    注意:选择排序是不稳定的。

    添加插入排序操作:

    #ifndef SORT_H
    #define SORT_H
    
    #include "Object.h"
    
    namespace DTLib
    {
    
    class Sort : public Object
    {
    private:
        Sort();
        Sort(const Sort&);
        Sort& operator = (const Sort&);
    
        template <typename T>
        static void Swap(T& a, T& b)
        {
            T c(a);
            a = b;
            b = c;
        }
    
    public:
        template < typename T >
        static void Select(T array[], int len, bool min2max=true)
        {
            for(int i = 0; i < len; i++)
            {
                int min = i;
                for(int j = i + 1; j < len; j++)
                {
                    if( min2max ? (array[min] > array[j]) : (array[min] < array[j]) )
                    {
                        min = j;
                    }
                }
    
                if( min != i)
                {
                    Swap(array[i], array[min]);
                }
            }
        }
    
        template < typename T >
        static void Insert(T array[], int len, bool min2max=true)
        {
            for(int i=1; i < len; i++)  //从1开始,第0个元素没有必要插入操作
            {
                int k = i;
                T e = array[i];
    
                for(int j=i-1; (j>=0) && (min2max ? (array[j] > e) : (array[j] < e)); j--)
                {
                    array[j+1] = array[j];
                    k = j;
                }
    
                if( k != i )   //赋值比“比较操作耗时”
                {
                    array[k] = e;
                }
            }
        }
    };
    
    }
    
    #endif // SORT_H
    

    注意:插入排序是稳定的排序

    选择排序的时间复杂度是O(n*n)

    插入排序的时间复杂度是O(n*n)

    小结:

      

      

      

      

  • 相关阅读:
    2020杭电HDU-6831多校第六场Fragrant numbers(区间DP打表)
    Gym 102219H-Are You Safe?(凸包求解+判断点与凸包关系)
    2020杭电HDU-6827多校第六场Road To The 3rd Building(找规律求期望)
    洛谷P1099&Noip 2007提高-树网的核(树直径上的尺取)
    2020杭电HDU-6832多校第六场A Very Easy Graph Problem(最短路转最小生成树+dfs)
    CodeForces 950D-A Leapfrog in the Array(打表找规律)
    使用odoo价格表[pricelist]对价格进行特别处理,如 .99
    odoo 10 生产自动领料
    安装odoo服务
    advanced validation on purchase.
  • 原文地址:https://www.cnblogs.com/lh03061238/p/13598357.html
Copyright © 2011-2022 走看看