当前位置: 首页 > news >繁体>[算法]用java实现堆操作

[算法]用java实现堆操作

问题描述:
(1)建堆:将数组A[1..n]变成一个最大堆。(课本6.3)
(2)堆排序:将一个堆中的元素按递减排序输出。
(3)用插入方法建堆:堆大小从1到n每次插入一个元素到堆中,直到n个元素入堆。(课本p83,6-1)

public class heap_Tools {//将一个元素插入堆中public static void insert(List<Integer> heap,int value){if(heap.size() == 0){     //0下标放置nullheap.add(0, null);heap.add(1,value);}else {heap.add(value);heapUp(heap,heap.size()-1);}}//插入后向上调整位置,转变为大顶堆public static void heapUp(List<Integer> heap,int index){if(index >1){int parent = index/2;if(heap.get(parent) < heap.get(index)){swap(heap, parent, index);heapUp(heap, parent);}}}//对大顶堆排序public static List<Integer> sort(List<Integer> heap){for(int i = heap.size()-1;i>0;i--){swap(heap, 1,i );adjust(heap, 1, i-1);;}return heap;}//生成并输出一个大顶堆public static List<Integer> adjust(List<Integer> heap){for(int i =heap.size()/2;i>0;i--){adjust(heap,i,heap.size()-1);}System.out.println("将数组转化为最大堆输出:");print_heap(heap);return heap;}public static void adjust(List<Integer> heap,int index,int n){int child = index*2;if((child+1)<=n&&heap.get(child)<heap.get(child+1)){  //判断如果左孩子<右孩子child +=1;}if(child<n&&heap.get(index)<heap.get(child)){swap(heap,index,child);}if(child<=n/2){adjust(heap, child, n);  //交换后,以child为根的子树不一定满足大顶堆定义,递推调整
        }}//交换List中的两个值public static void swap(List<Integer> heap,int a,int b) {int temp;temp = heap.get(a);heap.set(a, heap.get(b));heap.set(b, temp);}public static void print_heap(List<Integer> heap){int floors = 0;for(int i = 1;i<heap.size();i++){System.out.print(heap.get(i)+"\t");if(i == (2<<floors)-1){floors++;System.out.print("\n");}}}
}

二:下沉法获得最大堆

public class Max_heap {public static void main(String[] args){//下沉法获得最大堆int[] A =  {1,2,5,10,3,7,11,15,17,20,10,9,5,8,6};List<Integer> array = new ArrayList<Integer>();array.add(0, null);for(int i = 0; i<A.length;i++){array.add(A[i]);}heap_Tools.adjust(array);}
}

三:将堆按照从大到小输出

public class heap_sort {public static void main(String[] args){//下沉法获得最大堆int[] A =  {1,2,5,10,3,7,11,15,17,20,10,9,5,8,6};List<Integer> array = new ArrayList<Integer>();array.add(0, null);for(int i = 0; i<A.length;i++){array.add(A[i]);}List<Integer> heap = heap_Tools.adjust(array);heap = heap_Tools.sort(heap);System.out.println("将堆中元素按递减顺序输出:");for(int i = heap.size()-1;i>0;i--){System.out.print(heap.get(i)+"\t");}}
}

四:插入法建堆

public class insert_build_heap {public static void main(String[] args){//下沉法获得最大堆int[] A =  {1,2,5,10,3,7,11,15,17,20,10,9,5,8,6};List<Integer> array = new ArrayList<Integer>();for(int i = 0; i<A.length;i++){heap_Tools.insert(array,A[i]);}        System.out.println("插入建立的堆为:");heap_Tools.print_heap(array);}
}

转载于:https://www.cnblogs.com/gejuncheng/p/8795780.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:https://dhexx.cn/news/show-18035.html

如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网进行投诉反馈,一经查实,立即删除!


相关文章:

  • Python写一个服务
  • kafka传数据到Flink存储到mysql之Flink使用SQL语句聚合数据流(设置时间窗口,EventTime)...
  • springboot问题记录
  • 在做简单网页时,遇到的一些js问题
  • eclipse下的mybatis插件:MyBatipse
  • MyBatis mapper parameterType
  • 微店一键复制商品软件使用教程
  • bzoj 相似回文串 3350 3103 弦图染色+manacher
  • Oracle11g常用数据字典(转)
  • Python--网络编程-----基于UDP协议的套接字不会发生粘包
  • HTML-参考手册: HTML ASCII
  • Windows服务器搭建Redis
  • 性能测试题目
  • 双链表删除一个节点
  • 添加样式(后台给字段note(left,height-auto ))
  • #SQL1242错误
  • SSL-ZYC 2402 世界语
  • ActiveMQ 无法启动 提示端口被占用 解决方案
  • MySQL全量备份和增量备份脚本
  • HDU 2159 完全背包
  • 理解数据类型与数学运算:摄氏温度与华氏温度的相互转换
  • YouCompleteMe自动补全的安装配置与使用
  • [BZOJ5006][LOJ#2290][THUWC2017]随机二分图(概率+状压DP)
  • java基础-对象-练习集锦
  • WPF中,输入完密码回车提交 ,回车触发按钮点击事件
  • python实现文件批量添加重命名
  • linux随手笔记(Centos为主)
  • js表单验证 方法
  • luogu题解 UVA11992 【Fast Matrix Operations】
  • 错误与异常_1-5选择题
  • access窗体主体居中
  • 51nod 1268最大距离
  • 51nod 1285山峰和分段
  • expected at least 1 bean which qualifies as autowire candidate for this depe (spring无法注入)...
  • 20172330 2017-2018-1 《Java程序设计》第八周学习总结
  • node socketlog
  • JavaWeb学习笔记7--JSP脚本元素、指令元素、动作元素
  • BZOJ2395 [Balkan 2011]Timeismoney 【最小乘积生成树】
  • 测试小白的实习
  • js:防抖动与节流【转载】
  • Groovy闭包
  • pandas如何去掉时间列的小时只保留日期
  • 上周热点回顾(4.30-5.6)
  • spring-session实现分布式集群session的共享(转)
  • Ubuntu16.04LTS +Qt+boost1.66编译错误:consuming_buffers.hpp: parse error in template argument list...
  • Spring 下 MyBatis 的基本使用
  • 处事笔记
  • SSM框架搭建问题
  • 一次http请求中的信息
  • 浅谈css常用伪类用法