常见排序算法Java版(待续)

  1. 冒泡排序O(n^2)

    java 复制代码
    public class Main {
        public static void main(String[] args) {
            Random random = new Random();
            int[] nums = new int[]{random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100)};
            for (int i = nums.length - 1; i >= 0; i--) {
                for (int j = 0; j < i; j++) {
                    if (nums[j] > nums[j + 1]) {
                        int temp = nums[j];
                        nums[j] = nums[j + 1];
                        nums[j + 1] = temp;
                    }
                }
            }
            for (int num : nums) {
                System.out.print(num + " ");
            }
        }
    }
  2. 选择排序O(n^2),

    java 复制代码
    public class Main {
        public static void main(String[] args) {
            Random random = new Random();
            int[] nums = new int[]{random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100)};
            int index;
            for (int i = 0; i < nums.length; i++) {
                index = i;//每一轮记录最小值的索引
                for (int j = i + 1; j < nums.length; j++) {
                    if (nums[j] < nums[index]) {
                        index = j;
                    }
                }
                if (index != i) {
                    int temp = nums[i];
                    nums[i] = nums[index];
                    nums[index] = temp;
                }
            }
            for (int num : nums) {
                System.out.print(num + " ");
            }
        }
    }
  3. 插入排序O(n^2)

    java 复制代码
    public class Main {
        public static void main(String[] args) {
            Random random = new Random();
            int[] nums = new int[]{random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100)};
            int index;
            for (int i = 1; i < nums.length; i++) {
                int rec = nums[i];
                index = i;
                for (int j = i - 1; j >= 0; j--) {
                    if (nums[j] > rec) {
                        nums[index] = nums[j];
                        index = j;
                    } else {
                        break;
                    }
                }
                nums[index] = rec;
            }
            for (int num : nums) {
                System.out.print(num + " ");
            }
        }
    }
相关推荐
能摆一天是一天27 分钟前
JAVA stream().flatMap()
java·windows
焦耳加热1 小时前
阿德莱德大学Nat. Commun.:盐模板策略实现废弃塑料到单原子催化剂的高值转化,推动环境与能源催化应用
人工智能·算法·机器学习·能源·材料工程
wan5555cn1 小时前
多张图片生成视频模型技术深度解析
人工智能·笔记·深度学习·算法·音视频
颜如玉1 小时前
🤲🏻🤲🏻🤲🏻临时重定向一定要能重定向🤲🏻🤲🏻🤲🏻
java·http·源码
u6061 小时前
常用排序算法核心知识点梳理
算法·排序
程序员的世界你不懂3 小时前
【Flask】测试平台开发,新增说明书编写和展示功能 第二十三篇
java·前端·数据库
星空寻流年3 小时前
设计模式第一章(建造者模式)
java·设计模式·建造者模式
gb42152873 小时前
java中将租户ID包装为JSQLParser的StringValue表达式对象,JSQLParser指的是?
java·开发语言·python
曾经的三心草4 小时前
Python2-工具安装使用-anaconda-jupyter-PyCharm-Matplotlib
android·java·服务器
蒋星熠4 小时前
Flutter跨平台工程实践与原理透视:从渲染引擎到高质产物
开发语言·python·算法·flutter·设计模式·性能优化·硬件工程