当前位置:网站首页>【21天学习挑战赛】顺序查找
【21天学习挑战赛】顺序查找
2022-08-04 04:07:00 【Alex抱着爆米花】
活动地址:CSDN21天学习挑战赛
怕什么真理无穷,进一步有一份的欢喜。
【21天学习挑战赛】顺序查找
我为什么参与挑战赛
1,机缘
读到研一了,暑假器件打开私信发现这个挑战赛就鼓起勇气参加了。
2,期待的收获
A, 本人在华南理工大学攻读专硕,目前研究方向是图像恢复,从事深度学习相关工作,目标是从事Java后端开发。
B, 期待您的反馈,如果有什么不对的地方,欢迎指正!
C, 期待认识志同道合的领域同行或技术交流。
如果感觉博主的文章还不错的话,还请关注、点赞、收藏三连支持一下博主哦
什么是查找?
在计算机科学中定义为:在一些(有序的/无序的)数据元素中,通过一定的方法找出与给定关键字相同的数据元素的过程叫做查找。也就是说,我们是出于某种目的,给算法传入比如key(序号,别名等)或者value(具体的值),返回给我们想要的数据类型,那就是查找了。
顺序查找的定义
查找一个指定元素,遍历整个数据结构(比如表,树,集合等),来查找是否存在指定元素。存在则查找成功,扫描结束未找到指定元素则查找失败。
顺序查找的优劣
优势
简单,以一维数组为例,我就是从前往后一个for循环就解决了简单粗暴。顺序排序更适合于无序的小的数据量的数据结构。
劣势
时间复杂度高,以一维数据为例,顺序查找的时间效率是O(n),二维数组顺序查找的时间效率是O(n^2)
️ 算法实现
public class SequentalSearch {
public static void main(String[] args) {
int[] arr = {
1, 4, 6, 7, 11};
int key = 3;
int value = 11;
Integer search = Search(arr, null, 11);
if (search!=null) {
System.out.println("查找到的值: "+search);
}else {
System.out.println("没找到!");
}
}
/** * @param arr 输入数组 * @param key 如果不为空,返回对应value * @param value 如果不为空,返回对应key */
public static Integer Search(int[] arr, Integer key, Integer value) {
if (key == null && value == null) {
System.out.println("请输入key或value");
}
if (key != null && value != null) {
System.out.println("不可以同时输入key和value");
}
//输入key
if (key != null) {
for (int i = 0; i < arr.length; i++) {
if (i == key) {
return arr[i];
}
}
}
//输入value
if (value != null) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == value) {
return i;
}
}
}
//没有找到
return null;
}
}
如果觉得对你有帮助的话:
点赞,你的认可是我创作的动力!
️ 收藏,你的青睐是我努力的方向!
️ 评论,你的意见是我进步的财富!
边栏推荐
猜你喜欢
Hey, I had another fight with HR in the small group!
【机器学习】21天挑战赛学习笔记(一)
JVM笔记
千兆2光8电管理型工业以太网交换机WEB管理X-Ring一键环网交换机
逻辑漏洞----其他类型
[Ryerson emotional speaking/singing audiovisual dataset (RAVDESS)]
2 Gigabit Optical + 6 Gigabit Electric Rail Type Managed Industrial Ethernet Switch Supports X-Ring Redundant Ring One-key Ring Switch
PL/SQL Some Advanced Fundamental
Reproduce 20-character short domain name bypass
SQL注入中 #、 --+、 --%20、 %23是什么意思?
随机推荐
类如何只能静态分配和只能动态分配
Innovation and Integration | Huaqiu Empowerment Helps OpenHarmony Ecological Hardware Development and Landing
【Ryerson情感说话/歌唱视听数据集(RAVDESS) 】
Functions, recursion and simple dom operations
【id类型和NSObject指针 ObjectIve-C中】
Jenkins 导出、导入 Job Pipeline
6-port full Gigabit Layer 2 network managed industrial Ethernet switch Gigabit 2 optical 4 electrical fiber self-healing ERPS ring network switch
sql语句查询String类型字段小于10的怎么查
系统设计.如何设计一个秒杀系统(完整版 转)
Shell 函数
马尔可夫链
FPGA parsing B code----serial 3
mysql索引笔记
机器学习之视频学习【更新】
Oracle与Postgresql在PLSQL内事务回滚的重大差异
7-1 LVS+NAT load balancing cluster, NAT mode deployment
new Date converts strings into date formats Compatible with IE, how ie8 converts strings into date formats through new Date, how to replace strings in js, and explain the replace() method in detail
Use serve to build a local server
如何动态添加script依赖的脚本
Hey, I had another fight with HR in the small group!