吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1357|回复: 3
收起左侧

[Java 转载] 【Java】数据结构-选择排序法

[复制链接]
孤樱懶契 发表于 2021-6-24 22:01

选择排序法

  • 原理就是不断将剩下元素中最小的数拿出来

    • image-20210624133947347

选择排序算法执行截图

image-20210624142356230

(完整代码)选择排序算法基础代码

public class SelectionSort {

    private SelectionSort(){}

    public static <E extends Comparable<E>> void sort(E[] arr){  //泛型 扩展 compareable 接口

        // arr[0...i)是有序的; arr[i...n)是无序的
        for(int i = 0 ; i < arr.length; i++){

            // 选择arr[i……n)中最小值的索引
            int minIndex = i;
            for(int j = i; j < arr.length; j++){
                if(arr[j].compareTo(arr[minIndex]) < 0)  // 返回的是整型,小于0代表前者小于后者
                    minIndex=j; //存的最小值所对应的索引
            }

            swap(arr, i, minIndex);
        }
    }

    private static <E> void swap(E[] arr, int i , int j){

        E t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }

    public static void main(String[] args){

        Integer[] arr = {1, 4, 2, 3, 6, 5};
        SelectionSort.sort(arr);
        for(int e: arr)
            System.out.print(e + " ");
        System.out.println();
    }
}

选择排序算法“优化”类排序执行截图

image-20210624145050773

(完整代码)选择排序选择排序算法“优化”类成员数值排序

SelectionSort.java

public class SelectionSort {

    private SelectionSort(){}

    public static <E extends Comparable<E>> void sort(E[] arr){  //泛型 扩展 compareable 接口

        // arr[0...i)是有序的; arr[i...n)是无序的
        for(int i = 0 ; i < arr.length; i++){

            // 选择arr[i……n)中最小值的索引
            int minIndex = i;
            for(int j = i; j < arr.length; j++){
                if(arr[j].compareTo(arr[minIndex]) < 0)  // 返回的是整型,小于0代表前者小于后者
                    minIndex=j; //存的最小值所对应的索引
            }

            swap(arr, i, minIndex);
        }
    }

    private static <E> void swap(E[] arr, int i , int j){

        E t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }

    public static void main(String[] args){

        Integer[] arr = {1, 4, 2, 3, 6, 5};
        SelectionSort.sort(arr);
        for(int e: arr)
            System.out.print(e + " ");
        System.out.println();

        Student[] students = {new Student("Alice", 98),
                            new Student("Bob", 100),
                            new Student("Charles", 66)};
        SelectionSort.sort(students);
        for(Student student: students)
            System.out.print(student + " ");
        System.out.println();
    }
}

(Override)覆盖方法Student.java

public class Student implements Comparable<Student>{
    private String name;
    private int score;

    public Student(String name , int score){
        this.name = name;
        this.score = score;
    }

    @Override
    public int compareTo(Student another){

//        if(this.score < another.score)
//            return -1;
//        else if(this.score == another.score)
//            return 0;
//          return 1;
        return this.score - another.score; //优雅写法 从小到大按分数排序
//          return another.score - this.score;  //从大到小按分数进行排序
    }

    @Override
    public boolean equals(Object student){
        if(this == student)
            return true;
        if(student == null)
            return false;

        if(this.getClass() != student.getClass())
            return false;

        Student another = (Student)student;
        return this.name.equals(another.name);
    }

    @Override
    public String toString(){
        return String.format("Student(name: %s, score: %d)", name, score);
    }
}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

ytlk0535 发表于 2021-6-24 23:20
感谢楼主分享,备用以后学了
niusan521 发表于 2021-7-2 13:09
if(arr[j].compareTo(arr[minIndex]) < 0)    这是在作比较吗?
  compareTo(arr[minindex])  这是啥意思?
 楼主| 孤樱懶契 发表于 2021-7-2 16:43
niusan521 发表于 2021-7-2 13:09
if(arr[j].compareTo(arr[minIndex]) < 0)    这是在作比较吗?
  compareTo(arr[minindex])  这是啥意思 ...

因为设定是泛型
所以比较大小只能使用compareTo这个来进行大小比较
j 和 minIndex   如果j > min*  则返回>0
j 和 minIndex   如果j = min*  则返回=0
j 和 minIndex   如果j < min*  则返回<0
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-25 13:12

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表