1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public static void main(String[] args) { int[] arr = {6, 2, 8, 3, 2, 8, 4, 7, 5, 23, 8, 1}; System.out.println(Arrays.toString(arr)); int[] res = sort(arr); System.out.println(Arrays.toString(res)); }
private static int[] sort(int[] arr) {
for (int i = 1; i < arr.length; i++) { for (int j = i; j > 0 && arr[j] < arr[j - 1]; j--) { swap(arr, j, j - 1); System.out.println("phase :" + j + Arrays.toString(arr)); }
} return arr; }
private static void swap(int[] arr, int i, int j) { int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; }
|
能否参与评论,且看个人手段。