public class BubbleSort { public static void main(String[] args) { int[] nums = {5, 3, 8, 6, 4}; int temp; for (int i = 0; i < nums.length - 1; i++) { for (int j = 0; j < nums.length - i - 1; j++) { if (nums[j] > nums[j + 1]) { temp = nums[j]; nums[j] = nums[j + 1]; nums[j + 1] = temp; } } } System.out.println('排序后的数组为:'); for (int i = 0; i < nums.length; i++) { System.out.print(nums[i] + ' '); } } }

// 屌丝风格 public class BubbleSort { public static void main(String[] args) { int[] nums = {5, 3, 8, 6, 4}; int temp; for (int i = 0; i < nums.length - 1; i++) { for (int j = 0; j < nums.length - i - 1; j++) { if (nums[j] > nums[j + 1]) { temp = nums[j]; nums[j] = nums[j + 1]; nums[j + 1] = temp; } } } System.out.println('排序后的数组为:'); for (int i = 0; i < nums.length; i++) { System.out.print(nums[i] + ' '); } } }

Java 冒泡排序:从菜鸟到大佬的进阶之路

原文地址: https://www.cveoy.top/t/topic/mv2U 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录