public static int[] heapSort(int[] arr) { int i; for (i = arr.length / 2; i >= 0; i--) { heapAdjust(arr, i, arr.length - 1); } for (i = arr.length - 1; i > 0; i--){ swap(arr, 0, i); heapAdjust(arr, 0, i - 1); } return arr; } private static void heapAdjust(int[] arr, int s, int m) { int temp, j; temp = arr[s]; for (j = 2 * s; j <= m; j *= 2){ // 如果右节点比左节点大,当前节点移到右节点 if (j < m && arr[j] < arr[j + 1]){ // 指向右节点 j++; } if (temp >= arr[j]){ break; } arr[s] = arr[j]; s = j; } arr[s] = temp; } private static void swap(int[] arr, int heigh, int low) { int temp = arr[heigh]; arr[heigh] = arr[low]; arr[low] = temp; }