常见排序算法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 + " ");
            }
        }
    }
相关推荐
后端AI实验室1 小时前
用AI写代码,我差点把漏洞发上线:血泪总结的10个教训
java·ai
CoovallyAIHub1 小时前
Moonshine:比 Whisper 快 100 倍的端侧语音识别神器,Star 6.6K!
深度学习·算法·计算机视觉
CoovallyAIHub2 小时前
速度暴涨10倍、成本暴降6倍!Mercury 2用扩散取代自回归,重新定义LLM推理速度
深度学习·算法·计算机视觉
CoovallyAIHub2 小时前
实时视觉AI智能体框架来了!Vision Agents 狂揽7K Star,延迟低至30ms,YOLO+Gemini实时联动!
算法·架构·github
CoovallyAIHub3 小时前
开源:YOLO最强对手?D-FINE目标检测与实例分割框架深度解析
人工智能·算法·github
程序员清风3 小时前
小红书二面:Spring Boot的单例模式是如何实现的?
java·后端·面试
belhomme3 小时前
(面试题)Redis实现 IP 维度滑动窗口限流实践
java·面试
CoovallyAIHub3 小时前
OpenClaw:从“19万星标”到“行业封杀”,这只“赛博龙虾”究竟触动了谁的神经?
算法·架构·github
Be_Better3 小时前
学会与虚拟机对话---ASM
java
刀法如飞3 小时前
程序员必须知道的核心算法思想
算法·编程开发·算法思想