-
冒泡排序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
相关推荐
代码雕刻家1 天前
3.5.Maven-依赖管理-依赖配置&依赖传递!chen1 天前
MyBatis-plus拓展之字段类型处理器、自动填充和乐观锁故事和你911 天前
sdut-程序设计基础Ⅰ-实验五一维数组(8-13)像污秽一样1 天前
算法与设计与分析-习题4.2Jin、yz1 天前
JAVA 八股va学弟1 天前
Java 网络通信编程(6):视频通话pjw198809031 天前
Spring Framework 中文官方文档Storynone1 天前
【Day20】LeetCode:39. 组合总和,40. 组合总和II,131. 分割回文串jgyzl1 天前
2026.3.11MyBatis-Plus基本使用与思考明明如月学长1 天前
AI 更新太快学不过来?我用OpenClaw打造专属AI学习工作流