第47课 - 选择排序和插入排序

1、选择排序

        选择排序的基本思想 

             - 每次(例如第 i 次,i = 0, 1, …, n-2)从后面n-i个待排的数 

                据元素中选出关键字最小的元素, 作为有序元素序列第 i 

                个元素。 


        第i次选择排序示例

            


            

            


2、编程实验 

选择排序的实现 Sort::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 t(a);
        a = b;
        b = t;
    }
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

main.cpp

#include <iostream>
#include"Sort.h"

using namespace std;
using namespace DTLib;



int main()
{
    int array[] = {7,9,4,6,2};

    Sort::Select(array,5);

    for(int i=0;i<5;i++)
    {
        cout<<array[i]<<" ";
    }

    cout<<endl;

    Sort::Select(array,5,0);

    for(int i=0;i<5;i++)
    {
        cout<<array[i]<<" ";
    }

    cout<<endl;


    return 0;
}

                            


3、插入排序

        插入排序的基本思想

             - 当插入第i (i >= 1)个数据元素时,前面的V[0] , V[1] , …, 

                V[i-1]已经排好序;这时,用V[i]的关键字与V[i-1] , 

                V[i-2], … , V[0]的关键字进行比较,找到位置后将V[i]插入, 

                原来位置上的对象向后顺移。 


        第i次插入排序示例

        


    

        

    

4、编程实验 

插入排序的实现    Sort::Insert
#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 t(a);
        a = b;
        b = t;
    }
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++)
        {
            int k = i;
            T e = array[i];

//            for(int j=i-1;j>=0;j--)
//            {
//                if(array[j] > e)
//                {
//                    array[j+1] = array[j];
//                    k = j;
//                }
//                else
//                {
//                    break;
//                }
//            }
            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

main.cpp

#include <iostream>
#include"Sort.h"

using namespace std;
using namespace DTLib;



int main()
{
    int array[] = {7,9,4,6,2};

    Sort::Select(array,5);

    for(int i=0;i<5;i++)
    {
        cout<<array[i]<<" ";
    }

    cout<<endl;

    Sort::Select(array,5,0);

    for(int i=0;i<5;i++)
    {
        cout<<array[i]<<" ";
    }

    cout<<endl;

    Sort::Insert(array,5);

    for(int i=0;i<5;i++)
    {
        cout<<array[i]<<" ";
    }

    cout<<endl;

    Sort::Insert(array,5,0);

    for(int i=0;i<5;i++)
    {
        cout<<array[i]<<" ";
    }


    return 0;
}
                        


5、小结 

            选择排序每次选择未排元素中的最小元素 

            插入排序每次将第 i 个元素插入前面 i-1 个已排元素中 

            选择排序是不稳定的排序法,插入排序是稳定的排序方法 

            选择排序和插入排序的时间复杂度为O(n2)

猜你喜欢

转载自blog.csdn.net/qq_39654127/article/details/80418907