-
冒泡排序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
相关推荐
独泪了无痕32 分钟前
探索Pinyin4j-Java中的拼音转换利器道尔柯南2 小时前
C++ string 类详解:从常用接口到深拷贝,彻底掌握字符串处理kingx33 小时前
Bugku 3122 谷歌小恐龙题解:纯离线逆向 Vault 的 AES-GCM 加密资源(附 Python 解密器)Dream it possible!4 小时前
LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等)DevOpenClub5 小时前
职教高考控制线 API:招生类别枚举、筛选与空分数含义心抵鹊5 小时前
算法之双指针系列(一)十二5 小时前
【洛谷题解】P1083 [NOIP 2012 提高组] 借教室(二分答案 + 差分)露天赏雪6 小时前
第3篇:Sentinel熔断降级实战:大模型接口抖动时的服务兜底方案小此方6 小时前
[算法实战论:动态规划]回文串问题的dp解法,回文串问题的母题,以及以此为基础的派生题天天被压力6 小时前
【A股分钟级高频数据完全手册 #06】北交所分钟级数据接口:京市股票Pro版最新分时