47 选择排序和插入排序

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

 

 

 

 

添加选择排序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#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

测试程序如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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添加一个参数,支持从小到大排序和从大到小排序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#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

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

 

 

 

 

 

 

 

 

添加插入排序操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#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)

 

小结:

  

  

  

  

posted on   lh03061238  阅读(132)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示