import java.util.Arrays; public class bobbleSort { public static void main(String[] args) { int[] arr = {2,6,3,7,4,1,8,5,0,9}; // {2,3,6,4,1,7,5,0,8,9} int temp; boolean result = true; for(int i=0;i<arr.length;i++){ for(int j=0;j<arr.length-i-1;j++){ if(arr[j]>arr[j+1]){ temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; result = false; }
System.out.println("第"+i+"趟,第"+j+"次:"+Arrays.toString(arr)); } if(result){ System.out.println(Arrays.toString(arr)); break; }else { result = true; } } } }
标签:arr,temp,int,及其,冒泡排序,Arrays,result,优化 From: https://www.cnblogs.com/xjklmycw/p/17173958.html