//冒泡排序 public class Demo05 { public static void main(String[] args) { int[] arr = {4, 1, 5, 2, 3}; for (int i = 0; i < arr.length - 1; i++) {//外循环:控制比较轮数(数组长度-1) i:0,1,2,3 for (int j = 0; j < arr.length - 1 - i; j++) {//内循环:控制比较次数 //升序:较大的值往后面放 if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } //增强for for (int i : arr) { System.out.print(i+" "); } System.out.println(); //普通for循环 for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } }
标签:arr,JAVA,int,System,冒泡排序,++,out From: https://www.cnblogs.com/cmhcmx/p/17844908.html