1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| int[] arr = {2, 6, 8, 3, 2, 8, 4, 7, 5, 23, 8, 1}; System.out.println(Arrays.toString(arr)); int[] res = s2(arr); System.out.println(Arrays.toString(res));
}
private static int[] s2(int[] arr) { for (int i = arr.length - 1; i > 0; i--) { findMax(arr, i); }
return arr; }
private static void findMax(int[] arr, int index) { for (int i = 0; i < index; i++) { if (arr[i] > arr[i + 1]) { swap(arr, i, i + 1); } } }
private static void swap(int[] arr, int i, int j) { int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; }
private static int[] sort(int[] arr) { int cnt = 0; for (int i = 0; i < arr.length; i++) { for (int j = i + 1; j < arr.length; j++) { System.out.println("cnt:" + ++cnt); if (arr[i] > arr[j]) { swap(arr, i, j); } }
} return arr; }
|
能否参与评论,且看个人手段。