标签:htc 额外 排序 soft 最小堆 最大的 ges 归并排序 build
function maxHeapify(arr, i) { let largest; let left = i * 2; // leftChild let right = i * 2 + 1; // rightChild if( left <= arr.length && arr[i] < arr[left] ) { largest = left; } else { largest = i; } if( right <= arr.length && arr[largest] < arr[right] ) { largest = right; } if ( largest !== i ) { arr[i] = arr[i] + arr[largest];
arr[largest] = arr[i] - arr[largest];
arr[i] = arr[i] - arr[largest]; maxHeapify(arr, largest); } }
function buildMaxHeap(arr) { for (var i = arr.length / 2; i >= 1; i--) { maxHeapify(arr,i); } }
function heapSort(arr) { let arrSort = []; buildMaxHeap(arr); let length = arr.length; for (var i = length; i >= 2; i--) { arr[1] = arr[i]; arrSort.push(arr[i]); maxHeapify(arr, 1); } arrSort.push(arr[1]); return arrSort; }
标签:htc 额外 排序 soft 最小堆 最大的 ges 归并排序 build
原文地址:http://www.cnblogs.com/RRirring/p/6421732.html