。。。堆排序。。。

public static void heapSort(int[] nums) {
int length = nums.length;
heapify(nums);
int i = length - 1;
while (i >= 1) {
swap(nums, 0, i);
i--;
adjustHeap(nums, 0, i);
}
}

private static void heapify(int[] nums) {
int end = nums.length - 1;
for (int i = (end - 1) / 2; i >= 0; i--) {
adjustHeap(nums, i, end);
}
}

private static void adjustHeap(int[] nums, int i, int end) {
while (2 * i + 1 <= end) {
int left = 2 * i + 1;
int right = 2 * i + 2;
int index = left;
if (right <= end && nums[right] > nums[left]) {
index = right;
}
if (nums[index] > nums[i]) {
swap(nums, index, i);
i = index;
} else {
break;
}
}
}

private static void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
原文地址:https://www.cnblogs.com/yingmeng/p/15807352.html