随笔 - 581  文章 - 0 评论 - 48 阅读 - 131万
< 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

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
 * @desc: 选择排序
 * @author: 毛会懂
 * @create: 2020-12-23 13:52:00
 **/
public class Selection {
 
    public static void sort(Comparable[] arr){
        for(int i = 0; i < arr.length - 1;i++){
            int minIndex = i;
            for(int j = i + 1;j < arr.length;j++){
                if(isExchange(arr[minIndex],arr[j])){
                    minIndex = j;
                }
            }
            //没有选择到更小的data,则不交换
            if(i != minIndex) {
                swap(arr, i, minIndex);
            }
        }
    }
 
    private static Boolean isExchange(Comparable o1,Comparable o2){
        return o1.compareTo(o2) > 0;
    }
 
    private static void swap(Comparable[] arr,int i,int j){
        Comparable temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
 
//选择排序
public static void main(String[] args) {
    Integer[] arr = {10,8,20,30,5,7,4,12,40,30,1,2,4,3,100,5,32,45,23,66,45,7,55,79,6};
    Selection.sort(arr);
    Arrays.asList(arr).forEach(System.out::println);
 
    Person[] persions = {new Person("b",11),new Person("a",10),new Person("c",12),new Person("b",111),
            new Person("a",5),new Person("c",4)};
    Insertion.sort(persions);
    for (Person person : persions) {
        System.out.println(person);
    }
}
 
附:
/**
 * @desc: 实现Comparable接口
 * @author: 毛会懂
 * @create: 2020-12-23 13:36:00
 **/
public class Person implements Comparable<Person>{
    private String name;
 
    private Integer age;
 
    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Integer getAge() {
        return age;
    }
 
    public void setAge(Integer age) {
        this.age = age;
    }
 
    @Override
    public String toString() {
        return "Persion{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
 
    @Override
    public int compareTo(Person o) {
        return this.age - o.getAge();
    }
}

  

posted on   毛会懂  阅读(99)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
历史上的今天:
2019-12-23 INSERT IGNORE 与INSERT INTO的区别
点击右上角即可分享
微信分享提示