常见查找算法-JAVA实现

2023-11-01

package org.nxt.algorithm.search;
/**
 * the bean of comparable
 * @author nanxiaotao
 *
 */
public class ComparableBean implements Comparable<ComparableBean> {

	private int value;
	

	public int getValue() {
		return value;
	}



	public void setValue(int value) {
		this.value = value;
	}



	@Override
	public int compareTo(ComparableBean o) {
		if(this.value<o.getValue()) {
			return -1;
		}else if(this.value==o.getValue()) {
			return 0;
		}else {
			return 1;
		}
	}

}

1.线性查找

   适用场景:顺序存储或链接存储的线性表

   时间复杂度:O(n)

package org.nxt.algorithm.search;


/**
 * linear search
 * @author nanxiaotao
 *
 */
public class LinearSearch {
	
	/**
	 * search
	 * @param datas aggregate of the data
	 * @param target the object to look for
	 * @return
	 */
	public static ComparableBean search(ComparableBean [] datas,ComparableBean target) {
		
		ComparableBean result=null;//Object found
		
		for(int i=0;i<datas.length;i++) {
			//Traverse the collection
			ComparableBean item=datas[i];//get the element of a collection by index
			//compare the current element to the target object
			if(0==item.compareTo(target)) {
				//if they are equal,assign and break out of the loop
				result=item;
				break;
			}
		}
		
		return result;
		
	}

}

1.二分查找

   适用场景:已经有序

   时间复杂度:O(log2n)

package org.nxt.algorithm.search;
/**
 * Binary Search
 * @author nanxiaotao
 *
 */
public class BinarySearch {
	
	/**
	 * search
	 * @param datas datas aggregate of the data
	 * @param target target the object to look for
	 */
	public static ComparableBean search(ComparableBean [] datas,ComparableBean target) {
		
		ComparableBean result=null;//Object found
		
		int first=0;//the first index of collection
		
		int last=datas.length-1;//the last index of collection
		
		int mid;//the middle index of colleciton
		
		/**
		 * Traverse the collection
		 * suspensive condition:
		 *                     1:find the target
		 *                     2:the first index equals the last index
		 */
		while(result==null&&first<=last) {
			mid=(first+last)/2;//compute the middle index
			if(datas[mid].compareTo(target)==0) {
				//equal
				result=datas[mid];
			}else {
				if(datas[mid].compareTo(target)==-1) {
					//smaller
					first=first+1;
				}else {
					//bigger
					last=last-1;
				}
			}
			
		}
		
		return result;
		
	}

}

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

常见查找算法-JAVA实现 的相关文章

随机推荐

  • 前后端交互与接口设计学习

    目录 前言 介绍目录内容和目的 前后端交互概述 解释前后端交互的基本概念和作用 2 强调前后端分离的架构模式 2 前后端交互逻辑设计 1 讨论前后端交互的一般逻辑流程 2 探索不同的数据传输方式 3 提供实际案例加深理解 4 接口设计与文档
  • 太原理工大学python考试题总结

    已知x 1 2 3 那么x 3的值为 1 2 3 1 2 3 1 2 3 已知x 1 2 和y 3 4 那么x y的结果是 1 2 3 4 Python 不具备运行速度快的特点 具备扩展库丰富 跨平台 支持函数式编程的特点 Python是面
  • 15. 线性代数 - 克拉默法则

    文章目录 克拉默法则 矩阵运算 Hi 大家好 我是茶桁 上节课我们在最后提到了一个概念 克拉默法则 本节课 我们就来看看到底什么是克拉默法则 克拉默法则 之前的课程我们一直在强调 矩阵是线性方程组抽象的来的 那么既然我们抽象出来了 有没有一
  • 选中物体高亮显示(MR开发日志)

    业务逻辑 屏幕中央扫到物体 点亮该物体 离开物体 取消高亮 程序逻辑 射线选中物体 配合Outline Effect高亮显示物体 场景设置 下载插件Outline Effect 1 摄像机设置添加Outline Effect脚本 2 然后那
  • Linux防火墙端口

    在服务器上使用某些软件时需要开启相应的防火墙端口号 简单了解下Linux防火墙端口 防火墙策略 防火墙策略可以基于流量的源目地址 端口号 协议 应用等信息来定制 然后防火墙使用预先定制的策略规则监控出入的流量 若流量与某一条策略规则相匹配
  • strlen、strcpy、strcmp、strcat函数的实现

    目录 一 strlen函数的实现 二 strcpy函数的实现 三 strcmp函数的实现 四 strcat函数的实现 五 代码示例展示 strlen strcpy strcmp strcat四个函数都包含在 include
  • ios safari 开启无痕浏览(隐私模式)报QuotaExceededError: DOM Exception 22异常解决办法...

    检测safari是否开启无痕浏览 function var testKey test var storage window sessionStorage try storage setItem testKey testValue stora
  • 域适应(Domain Adaptation)综述

    根据李宏毅老师的视频所归纳的笔记 视频链接 https www bilibili com video BV1TL411p7Us spm id from 333 999 0 0 假设我们在训练集上训练黑底白字的手写数字集后 如下图 再把它用在
  • undefined symbol 问题解决记录(二)

    昨天上车自测本模块功能稳定性 顺便pull小弟分支 帮忙一起验证 结果小包上车后无法运行 一查发现一直报 undefined symbol XXXXXX 晚上下班后开始帮忙排查 今日记录以便后期回顾 前两年写过一篇关于undefined s
  • git clone 使用用户名和密码

    git clone 使用用户名和密码 一般git仓库的用户 都是用户名和密码登录 git clone命令如下 模板 git clone http 邮箱 或用户名 密码 仓库 git clone http username password
  • Java读取和写入Excel表格

    Java读取和写入Excel表格 1 绪论 2 JXL篇之程序范例 2 1 JXL 创建低版本Excel文件 2 2 JXL 读取低版本Excel文件 3 POI篇之程序范例 3 1 POI 创建低版本Excel文件 3 2 POI 读取低
  • 阿里云移动测试

    阿里云移动测试 买服务 上传APP 勾选需要的服务 提交之后 阿里云帮进行测试 这个是自动的还是人工的
  • [SQL]经典的sql语句

    一 基础 1 说明 创建数据库 CREATE DATABASE database name 2 说明 删除数据库 drop database dbname 3 说明 备份sql server 创建 备份数据的 device USE mast
  • getClass().getClassLoader()为null

    想获取resources下的文件 之前用过this getClass getClassLoader getResourceAsStream path 可以获取到 但最近的一个工程中需要在一个静态方法中获取该文件 没有了this 我直接用了C
  • Qt使用OpenGL实现立方体贴图

    效果如下 实现代码3个文件 TestWidget h TestWidget cpp main cpp TestWidget h ifndef TESTWIDGET H define TESTWIDGET H include
  • STM32F103-时钟树

    STM32F1 时钟树 参考 野火 零死角玩转STM32 F103指南者 时钟源 HSI 高速内部时钟 RC振荡器 频率为8MHz HSE 高速外部时钟 可接石英 陶瓷谐振器 或者接外部时钟源 频率范围为4MHz 16MHz LSI 低速内
  • redis 优缺点 使用场景

    1 使用redis有哪些好处 1 速度快 因为数据存在内存中 类似于HashMap HashMap的优势就是查找和操作的时间复杂度都是O 1 2 支持丰富数据类型 支持string list set sorted set hash 3 支持
  • BootStrap----table

    项目场景 需要涉及到BootStrap table表格的 因为最后也没有使用bootstrap 现在只是简单的整理一下搜集到的资料 问题描述 提示 对于参数信息的涉及和查阅 参考 8条消息 bootstrapTable常用参数与方法 王小小
  • ubuntu18.04在vscode中配置c++环境

    1 安装gcc g sudo apt install gcc sudo apt install g 检查是否安装成功 gcc verison g version 2 在vscode商店中安装c 插件 3 在根目录创建 vscode文件夹 然
  • 常见查找算法-JAVA实现

    package org nxt algorithm search the bean of comparable author nanxiaotao public class ComparableBean implements Compara