/** * 冒泡排序 */ public class BubbleSort2{ public static void main(String[] args) { int[] arr = {12, 23, 45, 67, 87, 3, 2, 1, 6}; int temp = 0; for(int j=0; j<arr.length-1; j++){ for(int i=0; i<arr.length-1-j; i++){ if(arr[i]>arr[i+1]){ temp = arr[i+1]; arr[i+1] = arr[i]; arr[i] = temp; } } } StringBuilder sb = new StringBuilder(); sb.append("["); for(int i=0; i<arr.length; i++){ if(i == arr.length-1){ sb.append(arr[i]).append("]"); }else { sb.append(arr[i]).append(","); } } System.out.println(sb.toString()); } }