• Java手写选择排序和算法案例拓展


    Java手写选择排序和算法案例拓展

    1. Mermanid代码表示思维导图解释实现思路原理

    开始
    设定初始最小值
    是否遍历完数组
    交换最小值和当前值
    结束

    选择排序的思路是将数组分为已排序部分和未排序部分,每次从未排序部分选择最小的元素并将其与未排序部分的第一个元素交换位置,然后将已排序部分扩展一个元素。重复这个过程直到整个数组排序完成。

    2. 选择排序的手写必要性

    手写选择排序有以下几个必要性:

    • 理解排序算法的原理和思路
    • 提高对算法的理解和掌握程度
    • 增强对编程语言的熟练度
    • 增加对代码的调试和优化能力

    3. 选择排序的市场调查

    选择排序是一种简单但效率较低的排序算法,适用于小规模数据的排序。在实际应用中,选择排序的市场需求相对较低,更多的是使用效率更高的排序算法,如快速排序、归并排序等。

    4. 选择排序的详细介绍和步骤

    选择排序的实现步骤如下:

    1. 设定初始最小值为数组的第一个元素。
    2. 遍历数组,从第二个元素开始,依次与当前最小值进行比较。
    3. 如果遍历到的元素比当前最小值小,则更新最小值的索引。
    4. 遍历完数组后,将最小值与未排序部分的第一个元素交换位置。
    5. 将已排序部分扩展一个元素,重复步骤2-5,直到整个数组排序完成。

    下面是选择排序的Java代码实现:

    public class SelectionSort {
        public static void selectionSort(int[] arr) {
            int n = arr.length;
            
            for (int i = 0; i < n - 1; i++) {
                int minIndex = i;
                
                for (int j = i + 1; j < n; j++) {
                    if (arr[j] < arr[minIndex]) {
                        minIndex = j;
                    }
                }
                
                int temp = arr[minIndex];
                arr[minIndex] = arr[i];
                arr[i] = temp;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    5. 手写实现总结和手写必要性

    通过手写实现选择排序,我们对算法的原理和实现步骤有了更深入的理解。手写实现可以提高我们对算法的掌握程度,并增强对编程语言的熟练度。此外,手写实现还可以帮助我们更好地理解和调试代码,提高代码的质量和效率。

    6. 完整代码

    public class SelectionSort {
        public static void selectionSort(int[] arr) {
            int n = arr.length;
            
            for (int i = 0; i < n - 1; i++) {
                int minIndex = i;
                
                for (int j = i + 1; j < n; j++) {
                    if (arr[j] < arr[minIndex]) {
                        minIndex = j;
                    }
                }
                
                int temp = arr[minIndex];
                arr[minIndex] = arr[i];
                arr[i] = temp;
            }
        }
        
        public static void main(String[] args) {
            int[] arr = {64, 25, 12, 22, 11};
            selectionSort(arr);
            System.out.println("排序后的数组:");
            for (int num : arr) {
                System.out.print(num + " ");
            }
        }
    }
    
    • 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

    7. 应用前景调研

    选择排序在实际应用中的前景相对较低,更多的是使用效率更高的排序算法。选择排序的时间复杂度为O(n^2),在大规模数据的排序中效率较低。因此,在需要对大规模数据进行排序的场景中,更常使用快速排序、归并排序等算法。

    8. 拓展应用案例

    案例1:按照学生成绩排序

    假设有一个学生类Student,包含姓名和成绩属性。我们可以使用选择排序对学生对象数组按照成绩进行排序。

    public class Student {
        private String name;
        private int score;
        
        public Student(String name, int score) {
            this.name = name;
            this.score = score;
        }
        
        public String getName() {
            return name;
        }
        
        public int getScore() {
            return score;
        }
    }
    
    public class SelectionSort {
        public static void selectionSort(Student[] students) {
            int n = students.length;
            
            for (int i = 0; i < n - 1; i++) {
                int minIndex = i;
                
                for (int j = i + 1; j < n; j++) {
                    if (students[j].getScore() < students[minIndex].getScore()) {
                        minIndex = j;
                    }
                }
                
                Student temp = students[minIndex];
                students[minIndex] = students[i];
                students[i] = temp;
            }
        }
        
        public static void main(String[] args) {
            Student[] students = {
                new Student("Alice", 80),
                new Student("Bob", 90),
                new Student("Charlie", 70)
            };
            
            selectionSort(students);
            
            System.out.println("按照成绩排序后的学生列表:");
            for (Student student : students) {
                System.out.println(student.getName() + " - " + student.getScore());
            }
        }
    }
    
    • 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

    案例2:选择排序优化

    选择排序的一个优化方法是使用双向选择排序,即同时找到最大值和最小值进行交换。这样可以减少比较和交换的次数。

    public class SelectionSort {
        public static void selectionSort(int[] arr) {
            int n = arr.length;
            int left = 0, right = n - 1;
            
            while (left < right) {
                int minIndex = left;
                int maxIndex = right;
                
                for (int i = left; i <= right; i++) {
                    if (arr[i] < arr[minIndex]) {
                        minIndex = i;
                    }
                    if (arr[i] > arr[maxIndex]) {
                        maxIndex = i;
                    }
                }
                
                int temp = arr[minIndex];
                arr[minIndex] = arr[left];
                arr[left] = temp;
                
                if (maxIndex == left) {
                    maxIndex = minIndex;
                }
                
                temp = arr[maxIndex];
                arr[maxIndex] = arr[right];
                arr[right] = temp;
                
                left++;
                right--;
            }
        }
        
        public static void main(String[] args) {
            int[] arr = {64, 25, 12, 22, 11};
            selectionSort(arr);
            System.out.println("排序后的数组:");
            for (int num : arr){
        System.out.print(num + " ");
    }
    
    • 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

    输出结果为:11 12 22 25 64

  • 相关阅读:
    CentOS7安装Nginx
    第十四届蓝桥杯(web应用开发)模拟赛2期 -大学组
    Elastic:推出 7.16.2 和 6.8.22 版本的 Elasticsearch 和 Logstash 以升级 Apache Log4j2
    win10系统怎样分区,win10固态硬盘怎么分区
    深入理解:输入流read()方法的底层运作原理,以及为什么缓存空间可以极大的提升IO流读写文件的效率
    MyBatis与Spring的集成
    编辑距离解析
    React antd组件Checkbox.Group单选实现
    使用docker搭建kafka集群、可视化操作台
    Spring-boot 操作失败,签名验证失败:X-TIMESTAMP已过期
  • 原文地址:https://blog.csdn.net/qq_22593423/article/details/132927731