parent= (i-1)/2 (向下取整);
c1 = 2i + 1;
c2 = 2i+2;
// 大根堆排序let tree = [4,10,3,5,1,2];let n = 6;heapify(tree,6,0);function build_heap(tree, n) {let last_node = n - 1;let parent = (last_node - 1) / 2;for (let i = parent; i >= 0; i--) {heapify(tree, n, i);}}function heapify(tree, n, i) {if (i >= n) {return;}let c1 = 2 * i + 1;let c2 = 2 * i + 2;let max = i;if (c1 < n && tree[ci] > tree[max]) {max = c1;}if (c2 < n && tree[c2] > tree[max]) {max = c2;}if (max !== i) {swap(tree, max, i);heapify(tree, n, max);}}function swap(tree, i, j) {let temp = tree[i];tree[i] = tree[j];tree[j] = temp;}function findKthLargest(nums,k) {let len = nums.length;build_heap(nums, len);let heapsize = len;for (let i = len; i >= len - k + 1; i--) {swap(nums, 0, i);heapsize--;heapify(nums, 0, heapsize);}return nums[0];}
