```java public static void descendingSort(int[] arr) { for (int i = 1; i < arr.length; i++) { swim(arr, i); } int size = arr.length; swap(arr, 0, --size); while (size > 0) { sink(arr, 0, size); swap(arr, 0, --size); } } private static void swim(int[] arr, int i) { while (i > 0 && arr[i] < arr[(i - 1) / 2]) { swap(arr, i, (i - 1) / 2); i = (i - 1) / 2; } } private static void sink(int[] arr, int i, int n) { while (2 * i + 1 < n) { int left = 2 * i + 1; int min = left; min = left + 1 < n && arr[left] > arr[left + 1] ? min + 1 : min; min = arr[i] <= arr[min] ? i : min; if (min == i) return; swap(arr, min, i); i = min; } } private static void swap(int[] arr, int i, int j) { if (i != j) { arr[i] = arr[i] ^ arr[j]; arr[j] = arr[i] ^ arr[j]; arr[i] = arr[i] ^ arr[j]; } } ```

相关推荐

牛客网
牛客企业服务