首页 > 其他分享 >堆排序用法

堆排序用法

时间:2022-11-13 04:44:05浏览次数:40  
标签:get int 堆排序 list maxIndex 用法 节点 size

因为堆结构只保证 根节点比双子节点都大或小

1   求最小的n个数:

      构建n个数的大顶堆,依次弹出堆顶再往下调整(用例省略)

2   求最大的n个数:

      构建n个数的小顶堆,依次弹出堆顶再往下调整(用例省略)

3   求第n大的数:

      构建所有元素的大顶堆,依次弹出堆顶,堆底放到堆顶并向下调整,重复n次

       用例:

        

public int findKth(int[] a, int n, int K) {
        ArrayList<Integer> list = (ArrayList<Integer>)Arrays.stream(a).boxed().collect(
                                      Collectors.toList());
        //大根堆
        buildHeap(list);
        int maxK = 0;
        for (int i = 0 ; i < K ; i++) {
            maxK = list.get(0);
            //顶换到底部
            swap(list, list.size() - 1, 0);
            //删除队尾元素
            list.remove(list.size() - 1);
            //向下调整
            shiftDown(list, 0);
        }
        return maxK;

    }
    private void buildHeap(ArrayList<Integer> a) {
        int len = a.size();
        for (int i = len / 2 - 1 ; i >= 0; i--) {
            shiftDown(a, i);
        }
    }
    private void shiftDown(ArrayList<Integer> a, int i) {
        System.out.println(a.size() + ";" + i);
        int len = a.size() - 1;
        int maxIndex = 0;
        
        if (2 * i + 2 > len) {
           //没有右子节点
           if(2*i+1 > len) {
              return;
           }else{
               //有左子节点
               maxIndex = 2 * i + 1;
           }
        } else {
            //有双子节点
            if (a.get(2 * i + 1) < a.get(2 * i + 2)) {
                //左子节点小于右子节点
                maxIndex = 2 * i + 2;
            } else {
                //左子节点大
                maxIndex = 2 * i + 1;
            }
        }


        if (a.get(i) < a.get(maxIndex)) {
            swap(a, i, maxIndex);
            shiftDown(a, maxIndex);
        }
    }
    private void swap(ArrayList<Integer> a, int i, int j) {
        int temp = a.get(i);
        a.set(i, a.get(j));
        a.set(j, temp);
    }

  

 

标签:get,int,堆排序,list,maxIndex,用法,节点,size
From: https://www.cnblogs.com/yanher/p/16885315.html

相关文章

  • datax同步数据java简单用法
    1.到github下载源码,自己编译。同步数据支持mysql8.0,如果直接用编译好的会遇到各种问题。https://github.com/alibaba/DataX/blob/master/userGuid.mdidea导入项目,需要先......
  • 事务 还有这些用法,之前都不知道
    #序transationTemplate.execute的写法第一次碰到,我之前是controller->biz->service->mapper然后用@Transation注解搞定事务,至于同一个类的方法之间调用,在bi......
  • Mysql中REPLACE INTO用法,判断数据是否存在,如果不存在,则插入,如果存在,则先删除此行数据,
    MySQLreplaceinto用法在向表中插入数据的时候,经常遇到这样的情况:1.首先判断数据是否存在;2.如果不存在,则插入;3.如果存在,则先删除后再插入新数据行。MySQL中实现这......
  • Vue中JSX的基本用法
    基本用法首先需要约定一下,使用JSX组件命名采用首字母大写的驼峰命名方式,样式可以少的可以直接基于vue-styled-components写在同一个文件中,复杂的建议放在单独的_Styles.js_......
  • 理解C++中 const 在指针中的用法
    intmain(){ int*constarray; constint*array; inta=10; array=&a;//Youcan'texchangearrayself,arrayjustisaintegar// *array=13;//Thisiserror......
  • c++ bit 库用法
    c++20加入了一个叫做bit的库,不如来看看里面有什么?bit_cast效果和reinterpret_cast类似,按二进制位取值,constexprfloatN=100;constexprintM=std::bit_cast<int>......
  • 【JS】8 种 ES6 中扩展运算符的用法
    扩展操作符 … 是ES6中引入的,将可迭代对象展开到其单独的元素中,所谓的可迭代对象就是任何能用forof循环进行遍历的对象,例如:数组、字符串、Map、Set、DOM节点等。1、拷贝......
  • 浅谈 c++20 ranges 的用法
    ranges库是c++20开始具有的语法,对应的头文件是#include<ranges>。为了防止CE我一般都这么写:#if__cplusplus>=202002L#include<ranges>usingnamespacestd::view......
  • console 简介与用法
    console简介与用法alert 弹窗早年没有console,用的都是 alert() 来调试;关于alert() 的几个小问题:alert(1,2,3);//只会弹出1;alert只有接受一个......
  • 【热更新实践】xLua基本用法
    这边文章是看xlua官方教程和一些文档之后的一个总结,希望大家都能学会lua,当然最希望我能快点学会lua。。。C#调用Lua(1)LuaEnvLuaEnv是C#中调用lua时需要用到的lua环境提示,需......