日撸 Java 三百行day48

news2024/9/21 14:49:49

文章目录

  • 说明
  • day48 堆排序
    • 1.基本思路
    • 2.代码

说明

闵老师的文章链接: 日撸 Java 三百行(总述)_minfanphd的博客-CSDN博客
自己也把手敲的代码放在了github上维护:https://github.com/fulisha-ok/sampledata

day48 堆排序

1.基本思路

(1)大/小顶堆:每一个结点值都大于(小于)或等于孩子结点(实际上堆可以看成一颗完全二叉树)

(2)堆排序:利用这种堆的结构进行排序。如:对一个待排序列进行堆排序,需要先建堆(大顶堆),这时我们可以结合大顶堆的性质,取堆顶元素,取完堆顶元素后我们要重新调整大顶堆,再取堆顶数据再调整。在这个过程会涉及建堆,调整堆。所以要清楚这个堆有什么特点。

(3)堆:堆通常是一个可以被看做一棵完全二叉树的数组对象,因为堆可以看做是完全二叉树。则完全二叉树的特点:

  • a. 假设完全二叉树总节点数为n,则会有n/2个父节点

  • b. 对一棵有n个结点的完全二叉树的结点按层序编号, 则对任一结点i (1≤i≤n) ,若2i>n,则结点i无左孩子,否则左孩子的结点为2i;如果2i+1>n,则结点i无右孩子,否则右孩子结点为2i+1

  • c. 完全二叉树的叶子结点也只能出现最后两层上

对于上面完全二叉树中a,b中这个关系对我们进行堆排序进行构建堆,调整堆很关键。

(4)手动模拟构造堆:(为了方便理解,我这里下标i从1开始, n=8)

  • step1:对待排序数组按完全二叉树方式排列出来; 调整堆:自下往上逐步调整。先找到最后一个父节点n/2=4,判断i=4结点的孩子结点较大是否大于父节点,若大于则交换数据

  • step2-5:继续向上调整,调整过程和step1一样,但是在调整的过程中,可能会破坏下一级的大顶堆结构,所以还需要向下检查,例如图中step4中i=1中53和87交换后,破坏了i=3处大顶堆的结构,则需要调整。

  • 在大顶堆构造好后,将根结点与最后一个结点交换位置后又重新开始调整堆(如下图)
    在这里插入图片描述
    用代码跑出来的堆排序结果:

-------heapSortTest-------
I am a data array with 8 items.
(53, if)  (17, then)  (78, else)  (9, switch)  (45, case)  (65, for)  (87, while)  (32, break)  
The parent position is 3 and the child is 7
Move 32 to position 3
Adjust 3 to 8: I am a data array with 8 items.
(53, if)  (17, then)  (78, else)  (32, break)  (45, case)  (65, for)  (87, while)  (9, switch)  
The parent position is 2 and the child is 6
Move 87 to position 2
Adjust 2 to 8: I am a data array with 8 items.
(53, if)  (17, then)  (87, while)  (32, break)  (45, case)  (65, for)  (78, else)  (9, switch)  
The parent position is 1 and the child is 4
Move 45 to position 1
Adjust 1 to 8: I am a data array with 8 items.
(53, if)  (45, case)  (87, while)  (32, break)  (17, then)  (65, for)  (78, else)  (9, switch)  
The parent position is 0 and the child is 2
Move 87 to position 0
The parent position is 2 and the child is 6
Move 78 to position 2
Adjust 0 to 8: I am a data array with 8 items.
(87, while)  (45, case)  (78, else)  (32, break)  (17, then)  (65, for)  (53, if)  (9, switch)  
The initial heap: I am a data array with 8 items.
(87, while)  (45, case)  (78, else)  (32, break)  (17, then)  (65, for)  (53, if)  (9, switch)  

The parent position is 0 and the child is 2
Move 78 to position 0
The parent position is 2 and the child is 5
Move 65 to position 2
Adjust 0 to 7: I am a data array with 8 items.
(78, else)  (45, case)  (65, for)  (32, break)  (17, then)  (9, switch)  (53, if)  (87, while)  
Round 1: I am a data array with 8 items.
(78, else)  (45, case)  (65, for)  (32, break)  (17, then)  (9, switch)  (53, if)  (87, while)  
The parent position is 0 and the child is 2
Move 65 to position 0
The parent position is 2 and the child is 5
Adjust 0 to 6: I am a data array with 8 items.
(65, for)  (45, case)  (53, if)  (32, break)  (17, then)  (9, switch)  (78, else)  (87, while)  
Round 2: I am a data array with 8 items.
(65, for)  (45, case)  (53, if)  (32, break)  (17, then)  (9, switch)  (78, else)  (87, while)  
The parent position is 0 and the child is 2
Move 53 to position 0
Adjust 0 to 5: I am a data array with 8 items.
(53, if)  (45, case)  (9, switch)  (32, break)  (17, then)  (65, for)  (78, else)  (87, while)  
Round 3: I am a data array with 8 items.
(53, if)  (45, case)  (9, switch)  (32, break)  (17, then)  (65, for)  (78, else)  (87, while)  
The parent position is 0 and the child is 1
Move 45 to position 0
The parent position is 1 and the child is 3
Move 32 to position 1
Adjust 0 to 4: I am a data array with 8 items.
(45, case)  (32, break)  (9, switch)  (17, then)  (53, if)  (65, for)  (78, else)  (87, while)  
Round 4: I am a data array with 8 items.
(45, case)  (32, break)  (9, switch)  (17, then)  (53, if)  (65, for)  (78, else)  (87, while)  
The parent position is 0 and the child is 1
Move 32 to position 0
Adjust 0 to 3: I am a data array with 8 items.
(32, break)  (17, then)  (9, switch)  (45, case)  (53, if)  (65, for)  (78, else)  (87, while)  
Round 5: I am a data array with 8 items.
(32, break)  (17, then)  (9, switch)  (45, case)  (53, if)  (65, for)  (78, else)  (87, while)  
The parent position is 0 and the child is 1
Move 17 to position 0
Adjust 0 to 2: I am a data array with 8 items.
(17, then)  (9, switch)  (32, break)  (45, case)  (53, if)  (65, for)  (78, else)  (87, while)  
Round 6: I am a data array with 8 items.
(17, then)  (9, switch)  (32, break)  (45, case)  (53, if)  (65, for)  (78, else)  (87, while)  
Adjust 0 to 1: I am a data array with 8 items.
(9, switch)  (17, then)  (32, break)  (45, case)  (53, if)  (65, for)  (78, else)  (87, while)  
Round 7: I am a data array with 8 items.
(9, switch)  (17, then)  (32, break)  (45, case)  (53, if)  (65, for)  (78, else)  (87, while)  
Result
I am a data array with 8 items.
(9, switch)  (17, then)  (32, break)  (45, case)  (53, if)  (65, for)  (78, else)  (87, while)  

2.代码

堆排序我觉得一定要借助图像,不然很难仅用脑子去想象这个过程。

  • (1)结合文章的代码,需要注意的是这里下标i不是从1开始的,是从0开始的,则i其左孩子结点为2i+1,右孩子结点为2i

  • (2)先看**adjustHeap(int paraStart, int paraLength)**这个方法中,for循环是根据i处父节点向下去筛选最大的结点,第一个if是判断父节点的孩子结点中的最大结点 第二个if则是与父节点进行比较,是否大于父节点,若大于则需要交换位置,在这里,交换了父节点并不能确定父节点的最终位置,因为可能交换了位置会破坏下面的堆结构,还需要向下继续调整。

  • (3)堆排序方法heapSort(),第一个for循环是构建初始堆,第二个循环则是在循环输出堆排序,我们从代码也可以看到,每取一个堆顶元素则要进行一次堆调整

  /**
     * heap sort. Maybe the most difficult sorting algorithm.
     */
    public void heapSort() {
        DataNode tempNode;
        //step1 Construct the initial heap
        for (int i = length/2 -1 ; i >= 0; i--){
            adjustHeap(i, length);
        }
        System.out.println("The initial heap: " + this + "\r\n");

        //step2 swap and reconstruct
        for (int i = length -1; i > 0; i--) {
            tempNode = data[0];
            data[0] = data[i];
            data[i] = tempNode;

            adjustHeap(0, i);
            System.out.println("Round " + (length - i) + ": " + this);
        }



    }

    /**
     * Adjust the heap
     * @param paraStart the start of the index
     * @param paraLength the length of the adjusted sequence
     */
    public void adjustHeap(int paraStart, int paraLength) {
        DataNode tempNode = data[paraStart];
        int tempParent = paraStart;
        int tempKey = data[paraStart].key;

        for (int tempChild = paraStart*2 + 1; tempChild <  paraLength; tempChild = tempChild*2 + 1) {
            //the right child is bigger
            if (tempChild + 1 < paraLength) {
                if (data[tempChild].key < data[tempChild+1].key) {
                    tempChild++;
                }
            }

            System.out.println("The parent position is " + tempParent + " and the child is " + tempChild);
            if (tempKey < data[tempChild].key) {
                // the child is bigger
                data[tempParent] = data[tempChild];
                System.out.println("Move " + data[tempChild].key + " to position " + tempParent);
                tempParent = tempChild;
            } else {
                break;
            }
        }

        data[tempParent] = tempNode;
        System.out.println("Adjust " + paraStart + " to " + paraLength + ": " + this);
    }

    public static void heapSortTest() {
        int[] tempUnsortedKeys = { 5, 3, 6, 10, 7, 1, 9 };
        //int[] tempUnsortedKeys = { 53, 17, 78, 9, 45, 65, 87, 32};
        String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };
       // String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" , "break"};
        DataArray tempDataArray = new DataArray(tempUnsortedKeys, tempContents);

        System.out.println(tempDataArray);

        tempDataArray.heapSort();
        System.out.println("Result\r\n" + tempDataArray);
    }

运行结果:

-------heapSortTest-------
I am a data array with 7 items.
(5, if)  (3, then)  (6, else)  (10, switch)  (7, case)  (1, for)  (9, while)  
The parent position is 2 and the child is 6
Move 9 to position 2
Adjust 2 to 7: I am a data array with 7 items.
(5, if)  (3, then)  (9, while)  (10, switch)  (7, case)  (1, for)  (6, else)  
The parent position is 1 and the child is 3
Move 10 to position 1
Adjust 1 to 7: I am a data array with 7 items.
(5, if)  (10, switch)  (9, while)  (3, then)  (7, case)  (1, for)  (6, else)  
The parent position is 0 and the child is 1
Move 10 to position 0
The parent position is 1 and the child is 4
Move 7 to position 1
Adjust 0 to 7: I am a data array with 7 items.
(10, switch)  (7, case)  (9, while)  (3, then)  (5, if)  (1, for)  (6, else)  
The initial heap: I am a data array with 7 items.
(10, switch)  (7, case)  (9, while)  (3, then)  (5, if)  (1, for)  (6, else)  

The parent position is 0 and the child is 2
Move 9 to position 0
The parent position is 2 and the child is 5
Adjust 0 to 6: I am a data array with 7 items.
(9, while)  (7, case)  (6, else)  (3, then)  (5, if)  (1, for)  (10, switch)  
Round 1: I am a data array with 7 items.
(9, while)  (7, case)  (6, else)  (3, then)  (5, if)  (1, for)  (10, switch)  
The parent position is 0 and the child is 1
Move 7 to position 0
The parent position is 1 and the child is 4
Move 5 to position 1
Adjust 0 to 5: I am a data array with 7 items.
(7, case)  (5, if)  (6, else)  (3, then)  (1, for)  (9, while)  (10, switch)  
Round 2: I am a data array with 7 items.
(7, case)  (5, if)  (6, else)  (3, then)  (1, for)  (9, while)  (10, switch)  
The parent position is 0 and the child is 2
Move 6 to position 0
Adjust 0 to 4: I am a data array with 7 items.
(6, else)  (5, if)  (1, for)  (3, then)  (7, case)  (9, while)  (10, switch)  
Round 3: I am a data array with 7 items.
(6, else)  (5, if)  (1, for)  (3, then)  (7, case)  (9, while)  (10, switch)  
The parent position is 0 and the child is 1
Move 5 to position 0
Adjust 0 to 3: I am a data array with 7 items.
(5, if)  (3, then)  (1, for)  (6, else)  (7, case)  (9, while)  (10, switch)  
Round 4: I am a data array with 7 items.
(5, if)  (3, then)  (1, for)  (6, else)  (7, case)  (9, while)  (10, switch)  
The parent position is 0 and the child is 1
Move 3 to position 0
Adjust 0 to 2: I am a data array with 7 items.
(3, then)  (1, for)  (5, if)  (6, else)  (7, case)  (9, while)  (10, switch)  
Round 5: I am a data array with 7 items.
(3, then)  (1, for)  (5, if)  (6, else)  (7, case)  (9, while)  (10, switch)  
Adjust 0 to 1: I am a data array with 7 items.
(1, for)  (3, then)  (5, if)  (6, else)  (7, case)  (9, while)  (10, switch)  
Round 6: I am a data array with 7 items.
(1, for)  (3, then)  (5, if)  (6, else)  (7, case)  (9, while)  (10, switch)  
Result
I am a data array with 7 items.
(1, for)  (3, then)  (5, if)  (6, else)  (7, case)  (9, while)  (10, switch)  

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

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

相关文章

【虚幻引擎】UE5 C++编译和打包失败的原因

一、出现The required library hostfxr.dll could not be found 错误 原因是缺少.NET Core3.1 解决办法一&#xff1a;可以去官网下载https://dotnet.microsoft.com/en-us/download/dotnet/3.1 解决方案二&#xff1a;打开Visual Studio Installer&#xff0c;选择单个组件&…

计算机Intel CPU体系结构分析

前段meldown漏洞事件的影响&#xff0c;那段时间也正好在读Paul的论文关于内存屏障的知识&#xff0c;其中有诸多细节想不通&#xff0c;便陷入无尽的煎熬和冥想中&#xff0c;看了**《计算机系统结构》、《深入理解计算机系统》、《大话处理器》**等经典书籍&#xff0c;也在g…

ISO9001是什么?ISO9000和ISO9001有何关系?

ISO 9000和ISO 9001是质量管理领域的两个重要标准。它们被用来确保组织能够提供符合客户要求的产品和服务&#xff0c;同时不断提高其业务效率和质量水平。本文将探讨ISO 9000和ISO 9001之间的关系&#xff0c;解释它们的区别以及为什么对企业非常重要。 什么是ISO9000和ISO90…

Windows安装Maven并配置环境

Windows下安装和配置Maven的步骤 介绍&#xff1a;步骤&#xff1a;步骤 1&#xff1a;下载Maven步骤 2&#xff1a;解压缩Maven分发包步骤 3&#xff1a;设置环境变量步骤 4&#xff1a;验证安装 结论&#xff1a; 介绍&#xff1a; Maven是一个非常流行的构建和项目管理工具…

Tunel技术是什么?

IPv4 用 32 位整数描述地址&#xff0c;最多只能支持 43 亿设备&#xff0c;显然是不够用的&#xff0c;这也被称作 IP 地址耗尽问题。为了解决这个问题&#xff0c;有一种可行的方法是拆分子网。拆分子网&#xff0c;会带来很多问题&#xff0c;比如说内外网数据交互&#xff…

银行业数字化运营体系(上):渠道触点矩阵建设

数字化运营体系是构建从获客、激活、留存、营收转化到转介的客户全生命周期的运营体系&#xff0c;推动线上产品和业务运营的数字化与智能化。 随着互联网技术的不断发展&#xff0c;移动设备已经成为人们日常生活中不可或缺的一部分&#xff0c;越来越多的用户在数字化渠道进行…

vmware 详细安装教程

一.VM是什么&#xff1f; VMware Workstation是一个“虚拟 PC”软件。它使你可以在一台机器上同时运行二个或更多 Windows、DOS、LINUX 系统。与“多启动”系统相比&#xff0c;VMWare 采用了完全不同的概念。多启动系统在一个时刻只能运行一个系统&#xff0c;在系统切换时需…

软考A计划-重点考点-专题六(数据库知识)

点击跳转专栏>Unity3D特效百例点击跳转专栏>案例项目实战源码点击跳转专栏>游戏脚本-辅助自动化点击跳转专栏>Android控件全解手册点击跳转专栏>Scratch编程案例 &#x1f449;关于作者 专注于Android/Unity和各种游戏开发技巧&#xff0c;以及各种资源分享&am…

数据结构实验的实验报告--B树

访问【WRITE-BUG数字空间】_[内附完整源码和文档] 数据结构实验的实验报告–B树 环境及工具 环境&#xff1a;C 工具&#xff1a;AnyivewCL B 定义 一棵 m 阶 B 树(Balance Tree of order m), 或为空树,或满足下列的特性的 m 叉树&#xff1a;(本次实验采用链式存储结构) …

SSM框架学习-加载properties文件

1. 创建新的命名空间 将 xmlns"http://www.springframework.org/schema/beans" 复制修改为 xmlns:context"http://www.springframework.org/schema/context" 再添加进去 表示开辟一个新的命名空间&#xff0c;叫做context 在xsi:schemaLocation中&#xff…

为什么耳鸣越来越年轻化了

耳鸣是一种声幻觉&#xff0c;当出现耳鸣的情况的时候&#xff0c;很多人都表示&#xff0c;耳朵总觉得听到很多奇怪的声音&#xff0c;甚至有时候还能听到车鸣笛声、或是轮船鸣笛声&#xff0c;但是附近并没有汽车轮船。 耳鸣&#xff0c;是一种没有外界声源情况下&#xff0c…

MapReduce框架原理

从源码的角度 :map --> sort —> copy --> sort -->reduce   sort —> copy --> sort属于shuffle InputFormat数据输入 切片与MapTask并行度决定机制 1&#xff09;问题引出 MapTask的并行度决定Map阶段的任务处理并发度&#xff0c;进而影响到整个Job的…

每日一个小技巧:1分钟告诉你视频转文字软件哪个好用

如果你曾经遇到过观看视频时无法边看边记笔记的尴尬&#xff0c;或是在听取讲座、会议录屏时错过关键词汇&#xff0c;那么这里有一个好的解决方法——使用视频转文字技术帮助你获取重要信息。通过这项技术&#xff0c;我们可以将视频中的内容转换成易于搜索和阅读的文字&#…

第一行代码 第八章 运用手机多媒体

第八章 运用手机多媒体 使用通知 通知&#xff08;Notification&#xff09;是Android系统中比较有特色的一个功能&#xff0c;当某个应用程序希望向用户发出一些提示信息&#xff0c;而该应用程序又不在前台运行时&#xff0c;就可以借助通知来实现。发出一条通知后&#xf…

2023年免费自动养站程序

什么是养站&#xff1f;SEO是与搜索引擎建立信任的过程&#xff0c;养站不仅仅是建立一个网站&#xff0c;还需要我们不断的更新和维护&#xff0c;才能使网站长时间稳定运行并获得更好的排名。今天跟大家分享如何建站以及如何养站。 一、明确TDK 在设计网站时&#xff0c;我…

无公网IP,公网SSH可远程访问家中的树莓派

文章目录 前言如何通过 SSH 连接到树莓派步骤1. 在 Raspberry Pi 上启用 SSH步骤2. 查找树莓派的 IP 地址步骤3. SSH 到你的树莓派步骤 4. 在任何地点访问家中的树莓派4.1 安装 Cpolar内网穿透4.2 cpolar进行token认证4.3 配置cpolar服务开机自启动4.4 查看映射到公网的隧道地址…

“土狗”的季节,meme热潮回归

文/章鱼哥 出品/陀螺财经 meme代币的热度好像又回来了&#xff0c;两周前推出的PEPE创下了历史新高。尽管加密货币市场仍处于漫长熊市中&#xff0c;但人们似乎仍然对风险投资保有兴趣。 meme代币作为基于互联网模因的高波动数字资产&#xff0c;似乎没有太多实用性。它们的价格…

屏幕录制大师哪款好用?这3款录屏工具,值得推荐!

案例&#xff1a;电脑录屏工具哪一款比较好用&#xff1f; 【我一般用手机拍电脑上需要录制的内容&#xff0c;但是拍出来的视频画面小且不清晰。朋友说可以使用电脑录屏软件录制电脑屏幕&#xff0c;但我以前没有用过&#xff0c;不知道哪款好用&#xff1f;家人们有没有推荐…

文件找回工具,值得推荐的4款!

例&#xff1a;文件找回工具 【对于我这种粗心大意经常丢失文件的人来说&#xff0c;有好用的文件找回工具实在太重要啦&#xff01;大家可以给我推荐一些好用的文件找回工具吗&#xff1f;感谢感谢&#xff01;】 当我们在使用电脑时&#xff0c;经常会遇到文件丢失的情况&a…

Java 如何在 Array 和 List 之间进行转换

概述 在本文章中&#xff0c;我们对如何在 Java 中对 Array 和 List 进行转换进行一些说明和示例。 这些示例通过使用 Core Java 和一些第三方的转换工具&#xff0c;例如 Guava 和 Apache Commons Collections。 更多有关的文章&#xff0c;请访问&#xff1a;Java - OSSEZ …