-
冒泡排序O(n^2)
javapublic 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 + " "); } } }
-
选择排序O(n^2),
javapublic 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 + " "); } } }
-
插入排序O(n^2)
javapublic 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 + " "); } } }
常见排序算法Java版(待续)
坤了2023-10-10 15:35
相关推荐
豆沙沙包?10 分钟前
2025年--Lc194-516. 最长回文子序列(动态规划在字符串的应用,需要二刷)--Java版胖咕噜的稞达鸭11 分钟前
二叉树搜索树插入,查找,删除,Key/Value二叉搜索树场景应用+源码实现_extraordinary_11 分钟前
Java Spring配置showmethetime11 分钟前
基于相空间重构的混沌时间序列预测MATLAB实现地平线开发者36 分钟前
大模型 | VLM 初识及在自动驾驶场景中的应用工业甲酰苯胺39 分钟前
Java并发机制的底层实现原理:从CPU到JVM的全面解析兩尛41 分钟前
java八股-操作系统码神本神44 分钟前
(附源码)基于Springboot的校园失物招领管理系统的设计与实现lingran__1 小时前
算法沉淀第四天(Winner)