日撸 Java 三百行day41

news2024/9/24 17:09:35

文章目录

  • 说明
  • day41 顺序查找与折半查找
    • 1.顺序查找
    • 2.折半查找
    • 3.代码

说明

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

day41 顺序查找与折半查找

1.顺序查找

从线性表的一边开始,逐个检查每个关键字是否满足条件。在顺序查中,引入了一个哨兵,他的作用:在数组0索引处存放我们需要查询的数据,引入哨兵的好处是在循环的过程中不用判断数组是否越界,因为当i==0时,循环一定会跳出。哨兵主要作用就是可以避免我们进行不必要的判断,借助这个思想在其他算法中也可以引入哨兵。

时间复杂度

  • 最好情况
    若第一个数据就是我们要查找的数据,则时间复杂度可以是O(1)
  • 最坏情况
    若最后一个数据或者我们查找的数据没在数组中,则需要遍历所以数据(一个for循环), 则时间复杂度可以是O(n)
  • 平均
    综合最好和最坏,则时间复杂度是O(n)

2.折半查找

一般的顺序查找是无序的,而折半查找 他主要适用的是有序的顺序查找,
思路:(假设顺序有升序排列)

  • 1.key值和中间位置的关键字比较,若相等查找成功
  • 2.若不相等,key值大于中间元素关键字,则查找元素可能在后半部分;若key值小于中间元素,则查找元素可能在前半部分。则缩小范围内查询
  • 对于折半查找我让我联想的到二叉排序树,二叉排序树的查找和二分查找很相似,但是二分查找他适用于有序的顺序表,而二叉排序树是树,有顺序存储和链式存储,之前在day22中给出的一种存储是压缩存储,所以二叉排序树的查找与二分查找的实现,还是要结合具体的存储结构

时间复杂度

  • 最好情况
    若第一个数据就是我们要查找的数据,则时间复杂度可以是O(1)
  • 最坏情况
    若最后一个数据或者我们查找的数据没在数组中,则需要需要进行 log n 次比较。所以折半查找的最坏时间复杂度为 O(log n)。
  • 平均
    综合最好和最坏,则时间复杂度是 O(log n)

顺序查找在最坏情况下需要将数组中所有的数据都遍历完,而折半查找每一次查找都会将待查找的元素范围缩小到一半,所以折半查找的时间复杂度要比顺序查找低。

3.代码

package graph;

import datastructure.queue.CircleObjectQueue;
import datastructure.stack.ObjectStack;
public class AdjacencyList {
    /**
     * An inner class for adjacent node.
     */
    class AdjacencyNode {
        /**
         * The column index.
         */
        int column;
        /**
         * The next adjacent node.
         */
        AdjacencyNode next;

        /**
         * The first constructor.
         *
         * @param paraColumn
         */
        public AdjacencyNode(int paraColumn) {
            column = paraColumn;
            next = null;
        }
    }

    int numNodes;
    AdjacencyNode[] headers;

    public AdjacencyList(int[][] paraMatrix) {
        numNodes = paraMatrix.length;

        // Step 1. Initialize. The data in the headers are not meaningful.
        AdjacencyNode tempPreviousNode, tempNode;

        headers = new AdjacencyNode[numNodes];
        for (int i = 0; i < numNodes; i++) {
            headers[i] = new AdjacencyNode(-1);
            tempPreviousNode = headers[i];
            for (int j = 0; j < numNodes; j++) {
                if (paraMatrix[i][j] == 0) {
                    continue;
                }

                tempNode = new AdjacencyNode(j);
                tempPreviousNode.next = tempNode;
                tempPreviousNode = tempNode;
            }
        }
    }

    @Override
    public String toString() {
        String resultString = "";
        AdjacencyNode tempNode;
        for (int i = 0; i < numNodes; i++) {
            tempNode = headers[i].next;

            while (tempNode != null) {
                resultString += " (" + i + ", " + tempNode.column + ")";
                tempNode = tempNode.next;
            } // Of while
            resultString += "\r\n";
        }
        return resultString;
    }


    boolean[] tempVisitedArray;
    String resultString = "";

    public String breadthFirstTraversal(int paraStartIndex) {
        CircleObjectQueue tempQueue = new CircleObjectQueue();
        String resultString = "";
        tempVisitedArray = new boolean[numNodes];

        tempVisitedArray[paraStartIndex] = true;

        // Initialize the queue.
        // Visit before enqueue.
        tempVisitedArray[paraStartIndex] = true;
        resultString += paraStartIndex;
        tempQueue.enqueue(new Integer(paraStartIndex));

        // Now visit the rest of the graph.
        int tempIndex;
        Integer tempInteger = (Integer) tempQueue.dequeue();
        AdjacencyNode tempNode;
        while (tempInteger != null) {
            tempIndex = tempInteger.intValue();

            // Enqueue all its unvisited neighbors. The neighbors are linked
            // already.
            tempNode = headers[tempIndex].next;
            while (tempNode != null) {
                if (!tempVisitedArray[tempNode.column]) {
                    // Visit before enqueue.
                    tempVisitedArray[tempNode.column] = true;
                    resultString += tempNode.column;
                    tempQueue.enqueue(new Integer(tempNode.column));
                } // Of if
                tempNode = tempNode.next;
            } // Of for i

            // Take out one from the head.
            tempInteger = (Integer) tempQueue.dequeue();
        } // Of while

        return resultString;
    }

    public String depthFirstTraversal(int paraStartIndex) {
        ObjectStack tempStack = new ObjectStack();
        resultString = "";
        tempVisitedArray = new boolean[numNodes];

        tempVisitedArray[paraStartIndex] = true;
        resultString += paraStartIndex;
        tempStack.push(new Integer(paraStartIndex));
        System.out.println("Push " + paraStartIndex);
        System.out.println("Visited " + resultString);

        int tempIndex = paraStartIndex;
        int tempNext;
        Integer tempInteger;
        AdjacencyNode tempNode;
        while (true) {
            tempNext = -1;
            // Find an unvisited neighbor and push
            tempNode = headers[tempIndex].next;
            while (tempNode != null) {
                if (!tempVisitedArray[tempNode.column]) {
                    // Visit before enqueue.
                    tempVisitedArray[tempNode.column] = true;
                    resultString += tempNode.column;
                    tempStack.push(new Integer(tempNode.column));
                    System.out.println("Push " + tempNode.column);
                    tempNext = tempNode.column;
                    break;
                } // Of if
                tempNode = tempNode.next;
            }

            if (tempNext == -1) {
                //there is no neighbor node, pop
                tempInteger = (Integer) tempStack.pop();
                System.out.println("Pop " + tempInteger);
                if (tempStack.isEmpty()) {
                    //No unvisited neighbor。Backtracking to the last one stored in the stack
                    break;
                } else {
                    tempInteger = (Integer) tempStack.pop();
                    tempIndex = tempInteger.intValue();
                    tempStack.push(tempInteger);
                }
            } else {
                tempIndex = tempNext;
            }
        }
        return resultString;
    }
    public boolean breadthTraversal(int paraStartIndex) {
        tempVisitedArray = new boolean[numNodes];
        resultString = "";
        breadthFirstTraversal(paraStartIndex);

        for (int i = 0; i < numNodes; i++){
            if (!tempVisitedArray[i]){
                breadthFirstTraversal(i);
                return false;
            }
        }
        return true;
    }

    public boolean depthTraversal(int paraStartIndex){
        tempVisitedArray = new boolean[numNodes];
        resultString = "";
        depthFirstTraversal(paraStartIndex);

        for (int i = 0; i < numNodes; i++){
            if (!tempVisitedArray[i]){
                depthFirstTraversal(i);
                return false;
            }
        }
        return true;
    }
    public static void breadthFirstTraversalTest() {
        // Test an undirected graph.
        //int[][] tempMatrix = { { 0, 1, 1, 0 }, { 1, 0, 0, 1 }, { 1, 0, 0, 0}, { 0, 1, 0, 0} };
         //int[][] tempMatrix = { { 0, 1, 1, 0 , 0}, { 1, 0, 0, 1, 0 }, { 1, 0, 0, 1, 0}, { 0, 1, 1, 0, 0}, { 0, 0, 0, 0, 0} };
        int[][] tempMatrix = { { 0, 1, 1, 0 , 0, 0, 0}, { 1, 0, 0, 1, 0, 0, 0 }, { 1, 0, 0, 1, 0, 0, 0}, { 0, 1, 1, 0, 0, 0, 0}, { 0, 0, 0, 0, 0, 1, 1}, { 0, 0, 0, 0, 1, 0, 0}, { 0, 0, 0, 0, 0, 0, 0} };
        Graph tempGraph = new Graph(tempMatrix);
        System.out.println(tempGraph);
        AdjacencyList tempAdjList = new AdjacencyList(tempMatrix);

        String tempSequence = "";
        try {
           // tempSequence = tempAdjList.breadthFirstTraversal(2);
            tempGraph.breadthTraversal(0);
        } catch (Exception ee) {
            System.out.println(ee);
        } // Of try.

        System.out.println("The breadth first order of visit: " + tempGraph.resultString);
    }
    public static void depthFirstTraversalTest() {
        // Test an undirected graph.
        //int[][] tempMatrix = { { 0, 1, 1, 0 }, { 1, 0, 0, 1 }, { 1, 0, 0, 0}, { 0, 1, 0, 0} };
        // int[][] tempMatrix = { { 0, 1, 1, 0 , 0}, { 1, 0, 0, 1, 0 }, { 1, 0, 0, 1, 0}, { 0, 1, 1, 0, 0}, { 0, 0, 0, 0, 0} };
        int[][] tempMatrix = { { 0, 1, 1, 0 , 0, 0, 0}, { 1, 0, 0, 1, 0, 0, 0 }, { 1, 0, 0, 1, 0, 0, 0}, { 0, 1, 1, 0, 0, 0, 0}, { 0, 0, 0, 0, 0, 1, 1}, { 0, 0, 0, 0, 1, 0, 0}, { 0, 0, 0, 0, 0, 0, 0} };
        Graph tempGraph = new Graph(tempMatrix);
        System.out.println(tempGraph);
        AdjacencyList tempAdjList = new AdjacencyList(tempMatrix);

        String tempSequence = "";
        try {
            tempGraph.depthTraversal(0);
           // tempSequence = tempAdjList.depthFirstTraversal(2);
        } catch (Exception ee) {
            System.out.println(ee);
        } // Of try.

        System.out.println("The depth first order of visit: " + tempGraph.resultString);
    }

    public static void main(String args[]) {
        int[][] tempMatrix = { { 0, 1, 0 }, { 1, 0, 1 }, { 0, 1, 0 } };
        AdjacencyList tempTable = new AdjacencyList(tempMatrix);
        System.out.println("The data are:\r\n" + tempTable);

        breadthFirstTraversalTest();

        depthFirstTraversalTest();
    }
}

在这里插入图片描述

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

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

相关文章

微信小程序蓝牙功能开发与问题记录

一、蓝牙支持情况 1. 微信小程序对蓝牙的支持情况 目前普遍使用的蓝牙规格&#xff1a;经典蓝牙和蓝牙低功耗。 经典蓝牙&#xff08;蓝牙基础率/增强数据率&#xff09;&#xff1a;常用在对数据传输带宽有一定要求的大数据量传输场景上&#xff0c;比如需要传输音频数据的…

AI工具和用法汇总—集合的集合

AI 工具和用法汇总 汇集整理 by Staok/瞰百&#xff0c;源于相关资料在我这慢慢越积累越多&#xff0c;到了不得不梳理的程度。 文中有许多内容作者还没有亲自尝试&#xff0c;所以很多内容只是罗列&#xff0c;但信息大源都已给出&#xff0c;授人以渔&#xff0c;欢迎 PR 补…

hadoop3.2.1+hive3.1.2-docker安装

Hadoop 1.拉取镜像 docker pull hadoop_hive:32.运行容器 建立hadoop用的内部网络(此步出错&#xff0c;若与其它网段冲突&#xff0c;可省略) #指定固定ip号段 docker network create --driverbridge --subnet172.17.0.1/16 hadoop建立Master容器&#xff0c;映射端口 10…

滚动加载数据

效果图: 综合使用后,觉得还是以下绑定 div监听滚动条的方式好用,有的可以监听滚轮滚动,但是监听不到鼠标拖动滚动条,下面这种方式两者都可以监测到↓ <template><div class"scrollTest-container" id"scrollTestContainer"><div class&quo…

简单分享微信小程序上的招聘链接怎么做

招聘小程序的主要用户就是企业招聘端和找工作人员的用户端,下面从这两个端来对招聘小程序开发的功能进行介绍。 企业端功能 1、岗位发布:企业根据自身岗位需求,在招聘app上发布招聘岗位及所需技能。 2.简历筛选:根据求职者提交的简历选择合适的简历,并对公开发布的简历进行筛…

105.(cesium篇)cesium指南针

听老人家说:多看美女会长寿 地图之家总目录(订阅之前建议先查看该博客) 文章末尾处提供保证可运行完整代码包,运行如有问题,可“私信”博主。 效果如下所示: 下面献上完整代码,代码重要位置会做相应解释 <html lang="en">

【软考高级】2019年系统分析师综合知识

第 1 题 面向对象分析中&#xff0c;一个事物发生变化会影响另一个事物&#xff0c;两个事物之间属于&#xff08;1&#xff09;。 (1) A .关联关系 B .依赖关系 C .实现关系 D .泛化关系 参考答案&#xff1a;(1)B 试题解析&#xff1a; 本题考查的是 UML 图中类的关系…

业务零中断,数据零丢失|庚顿新一代双活高可用架构实时数据库为流程工业核心业务保驾护航

新一代双活架构高可用架构实时数据库管理系统可实现流程工业数据平台“零中断”、“零丢数”的超高可用性要求&#xff0c;在满足实时性要求的同时&#xff0c;实现断网/掉电时业务不中断、不丢数&#xff0c;突破传统主备架构。 随着生产生活自动化、数字化、信息化水平不断升…

二叉树或者多叉树直径问题

原题链接&#xff1a;543. 二叉树的直径 - 力扣&#xff08;LeetCode&#xff09; 题目描述&#xff1a; 给定一棵二叉树&#xff0c;你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过也可能不穿过根结点。 什么是任意两点路径…

ChatGPT来了不用慌,广告人还有这个神器在手

#ChatGPT能取代广告人吗&#xff0c;#ChatGPT会抢走你的工作吗&#xff1f;#ChatGPT火了&#xff0c;会让营销人失业吗&#xff1f;自ChatGPT爆火以来&#xff0c;各种专业or非专业文章不停给广告人强加焦虑&#xff0c;但工具出现的意义&#xff0c;更多在于提效而非替代&…

BetaFlight统一硬件配置文件研读之timer命令

BetaFlight统一硬件配置文件研读之timer命令 1. 源由2. 代码分析3. 实例分析4. 配置情况4.1 AFn配置查表4.2 timer4.3 timer show4.4 timer pin list 5. 参考资料 统一硬件配置文件的设计是一种非常好的设计模式&#xff0c;可以将硬件和软件的工作进行解耦。 1. 源由 cli命令…

某程序员哀叹:二本计算机,4年开发,年包才40多万。二本真的不如985/211吗?

前段时间&#xff0c;某职场论坛上有人发了一个帖子&#xff0c;发帖人问&#xff1a;为什么大家工资那么高&#xff0c;三五年都六七十万了&#xff1f;我二本计算机专业&#xff0c;四年前端开发&#xff0c;找个年包40万多点就顶头了。 原贴如下&#xff1a; 有网友表示楼主…

【Docker】Docker上篇

文章目录 一、认识Docker1、Docker出现的背景2、Docker的历史3、虚拟机技术与容器技术4、容器比虚拟机快的原因5、对Devops层面的影响 二、Docker的安装的原理1、核心名词2、安装Docker&#xff08;for Linux&#xff09;3、配置阿里云镜像加速4、Run的流程和Docker原理 三、Do…

phoenix使用(一)之全局索引的使用

索引使用 1.1. 全局索引&#xff08;Global Indexing&#xff09; 名词解释&#xff1a; 全局索引&#xff0c;适用于读多写少的业务场景。使用Global indexing在写数据的时候开销很大&#xff0c;因为所有对数据表的更新操作&#xff08;DELETE, UPSERT VALUES and UPSERT SEL…

chatgpt软件 - chatbox

文章目录 打开github 进入chatgpt官方要记得登录&#xff01;&#xff01;点击头像将key命名&#xff1a;安装chatbox下面就可以开始使用啦&#xff01;&#xff01; 打开github https://github.com/Bin-Huang/chatbox 特性&#xff1a; 更自由、更强大的 Prompt 能力数据存储…

看了这13个案例,我总算又get到了企业级无代码

企业级无代码为何面向软件公司的研发团队&#xff1f; 哪些人能在企业级无代码旅程中获益&#xff1f; 如何找到实践的切入点&#xff1f; 如何开展规模化的交付实践&#xff1f; 如何快速组建企业级无代码开发团队&#xff1f; ...... 近期smardaten从组织创新、技术创新…

OCC的拓扑基础数据结构

在OpenCASCADE中,提供了一系列的拓扑基础数据结构,用于表示几何实体的拓扑结构,其中最基本的是TopoDS_Shape。下面是一些其他常用的拓扑数据结构: TopoDS_TCompound:代表了复合实体,即由多个几何实体组合而成的实体,可以包含任意数量和类型的其他几何实体。 TopoDS_TCom…

【消费战略方法论】认识消费者的恒常原理(三):消费者刺激反馈原理

人类是一种高度智能的生物&#xff0c;而所谓智能的核心在于其理解世界的能力&#xff0c;而理解世界的过程中必然伴随着感知和反应。人的刺激反馈机制就是在这个过程中发挥着重要的作用。 刺激反馈机制是一种生物学的反应现象&#xff0c;它指的是人体对外界刺激的感知与反应…

vue使用富文本和打印 egg使用ctx.getFileStream进行文件上传

vue2使用富文本 安装 npm install vue-quill-editor --save全局引入(main) import VueQuillEditor from vue-quill-editor//调用编辑器 // 样式 import quill/dist/quill.core.css import quill/dist/quill.snow.css import quill/dist/quill.bubble.css Vue.use(VueQuillE…

【汽车电子】浅谈LIN总线

目录 1.为何使用LIN总线 2.什么是LIN总线&#xff1f; 3.LIN总线的主从关系 4.LIN的特点 5.LIN报文帧结构 6.LIN总线波形 7.帧类型 8.进度表 9.状态机的实现 10.总结 11.声明 1.为何使用LIN总线 在这里你可能要问“不都有CAN总线了吗&#xff1f;这个LIN总线又是从哪…