zoukankan      html  css  js  c++  java
  • 大顶堆和堆排序

    function GetLeastNumbers_Solution(input, k)
    {
        // 构建最大堆
        // 二叉堆使用数组表示,顶点是下标1开始,子节点分别为2*n, 2*n +1
        // 对于长度为N的数组,其最后一个非叶子节点的位置为Math.floor(n/2)
        // 最大堆要求每一个分支都是最大堆,从最后一个父节点开始构建
        let n = input.length;
        if(input.length < k) return []
        // 构建最大堆
        for(let i=~~((n-1)/2); i>=0; i--) {
            maxHeapify(input, i, n);
        }
        // 堆排序
        for(let i=n-1; i>0; i--) {
            swap(input, 0, i);
            // 最大堆顶点和尾点交换后,将顶点最大堆化,则得到第二大。依次。。。
            maxHeapify(input, 0, i)
        }
        return input.slice(0,k);
    }
    // 
    function maxHeapify(input, i, n) {// i开始堆化的位置;n长度
        let left = 2*i + 1; 
        let right = 2*i + 2;
        // 默认父节点为最小值
        let maxIndex = i;
        if (left <n && input[maxIndex] < input[left]) {
            maxIndex = left;
        }
        if (right <n && input[maxIndex] < input[right]) {
            maxIndex = right;
        }
        if (maxIndex !== i) {
           swap(input, i, maxIndex); 
           // 对于变动后的节点,需要重新进行最大堆调整
           maxHeapify(input, maxIndex, n);
        }
    }
    function swap(arr, i, j) {                                      
        let temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
  • 相关阅读:
    CodeForces 670E Correct Bracket Sequence Editor
    CodeForces 670F Restore a Number
    HDU 5895 Mathematician QSC
    HDU 5880 Family View
    HDU 5886 Tower Defence
    CSS3.16
    CSS页面定制代码+动漫人物设计
    TSP变形(三进制状压)
    三进制状压(涂抹果酱)
    最小点覆盖(König定理)
  • 原文地址:https://www.cnblogs.com/lyraLee/p/12527500.html
Copyright © 2011-2022 走看看