public class StringSampleDemo { public static void main(String[] args) { int[] arr = {1, 2, -20, 20, 6, 7, 90}; int num = arr.length; //经过多少趟排序 for (int k = num - 1; k > 0; k--) { int flag = 0; //每一次比较逻辑 for (int i = 0; i < k; i++) { if (arr[i] > arr[i + 1]) { int temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; flag = 1; } } if (flag == 0) { break; } } for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } /* * -20 1 2 6 7 20 90 * */ } }