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

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

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

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

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

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

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

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

3. 选择排序的市场调查

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

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

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

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

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

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;
        }
    }
}

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

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

6. 完整代码

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;
        }
    }
    
    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 + " ");
        }
    }
}

7. 应用前景调研

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

8. 拓展应用案例

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

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

java 复制代码
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());
        }
    }
}

案例2:选择排序优化

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

java 复制代码
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 + " ");
}

输出结果为:11 12 22 25 64

相关推荐
Mem0rin8 分钟前
二分查找:左右边界
数据结构·算法
艾莉丝努力练剑13 分钟前
【QT:解决问题】Qt5Core.dll:无法定位程序输入点
java·开发语言·qt·学习·面试
小蒜学长22 分钟前
“喵汪联盟”宠物领养系统的设计与实现(代码+数据库+LW)
java·spring boot·后端·宠物
程序员黑豆24 分钟前
Java字符串常量池完全指南:原理、intern()方法与性能优化最佳实践
java·前端·ai编程
AI人工智能+电脑小能手27 分钟前
大白话说Java设计模式-14-适配器模式(业务实战篇)
java·设计模式·适配器模式·系统兼容·多渠道对接
xbgRS3 小时前
springboot的自动装配
java·spring boot
大明者省10 小时前
WSL2 Ubuntu22.04 GPU训练环境配置指南
人工智能·算法·计算机视觉
嘻哈∠※10 小时前
0061基于 SpringBoot 的投稿与稿件处理系统设计与实现
java·spring boot·后端
白狐_79811 小时前
408数据结构第8章:排序②——性质对比秒杀、场景选择与外部排序
java·数据结构·算法
zander25812 小时前
LeetCode 84:柱状图中的最大矩形——单调栈如何确定左右边界
java·数据结构·算法