-
冒泡排序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
相关推荐
每天进步一点_JL25 分钟前
JVM 类加载:双亲委派机制NAGNIP1 小时前
大模型框架性能优化策略:延迟、吞吐量与成本权衡用户298698530141 小时前
Java HTML 转 Word 完整指南渣哥1 小时前
原来公平锁和非公平锁差别这么大渣哥1 小时前
99% 的人没搞懂:Semaphore 到底是干啥的?J2K1 小时前
JDK都25了,你还没用过ZGC?那真得补补课了kfyty7251 小时前
不依赖第三方,不销毁重建,loveqq 框架如何原生实现动态线程池?美团技术团队2 小时前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型isysc13 小时前
面了一个校招生,竟然说我是老古董道可到6 小时前
Java 反射现代实践速查表(JDK 11+/17+)