-
冒泡排序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 小时前
使用python向钉钉群聊发送消息好奇龙猫2 小时前
【学习AI-相关路程-mnist手写数字分类-win-硬件:windows-自我学习AI-实验步骤-全连接神经网络(BPnetwork)-操作流程(3) 】P.H. Infinity2 小时前
【RabbitMQ】04-发送者可靠性生命几十年3万天2 小时前
java的threadlocal为何内存泄漏sp_fyf_20242 小时前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-11-01caridle3 小时前
教程:使用 InterBase Express 访问数据库(五):TIBTransaction^velpro^3 小时前
数据库连接池的创建苹果醋33 小时前
Java8->Java19的初步探索秋の花3 小时前
【JAVA基础】Java集合基础香菜大丸3 小时前
链表的归并排序