常见排序算法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 + " ");
            }
        }
    }
相关推荐
sszdlbw4 分钟前
后端springboot框架入门学习--第二篇
java·spring boot·学习
阿拉斯攀登6 分钟前
MyBatis 全面解析 & Spring Boot 集成实战
java·spring boot·mybatis·持久层框架
A尘埃7 分钟前
Java业务场景(高并发+高可用+分布式)
java·开发语言·分布式
白仑色12 分钟前
java中的anyMatch和allMatch方法
java·linux·windows·anymatch·allmatch
NeDon12 分钟前
[OJ]数据结构:移除链表元素
c语言·数据结构·算法·链表
刃神太酷啦13 分钟前
C++ list 容器全解析:从构造到模拟实现的深度探索----《Hello C++ Wrold!》(16)--(C/C++)
java·c语言·c++·qt·算法·leetcode·list
wearegogog12314 分钟前
C# 条码打印程序(一维码 + 二维码)
java·开发语言·c#
码农阿豪14 分钟前
用 PlaylistDL 攒私人音乐库?加个 cpolar,出门在外也能随时听!
java
LaughingDangZi14 分钟前
vue+java分离项目实现微信公众号开发全流程梳理
java·前端·后端
承渊政道14 分钟前
一文彻底搞清楚链表算法实战大揭秘和双向链表实现
c语言·数据结构·算法·leetcode·链表·visual studio