无序线性搜索(Unordered Linear Search)
假定有一个元素顺序情况不明的数组。这种情况如果我们要搜索一个元素就要遍历整个数组,才能知道这个元素是否在数组中。
这种方法要检查整个数组,核对每个元素。下面是算法实现:
#include<stdio.h> // a function to search "data" in an array "arr" of size "size"
// returns 1 if the element is present else 0
int unorderedLinearSearch(int arr[], int size, int data)
{
int found_flag = 0; int i;
for(i=0;i<size;i++)
{
//loop through the entire array and search for the element
if(arr[i] == data)
{
// if the element is found, we change the flag and break the loop
found_flag = 1;
break;
}
} return found_flag;
} //driver program to test the function
int main(void)
{
int arr[10] = {2, 6, 4, 10, 8, 1, 9, 5, 3, 7}; int to_search = 5; if(unorderedLinearSearch(arr,10,to_search))
printf("FOUND");
else
printf("NOT FOUND"); return 0;
}
时间复杂度(Time Complexity):O(n),这是遍历整个数组最差的情况。
空间复杂度(Space Complexity):O(1)。
无序线性搜索(Unordered Linear Search)的更多相关文章
- 有序线性搜索(Sorted/Ordered Linear Search)
如果数组元素已经排过序(升序),那我们搜索某个元素就不必遍历整个数组了.在下面给出的算法代码中,到任何一点,假设当前的arr[i]值大于搜索的值data,就可以停止搜索了. #include<s ...
- Linear Search
Search I You are given a sequence of n integers S and a sequence of different q integers T. Write a ...
- [Algorithms] Refactor a Linear Search into a Binary Search with JavaScript
Binary search is an algorithm that accepts a sorted list and returns a search element from the list. ...
- 搜索和搜索形式(SEARCHING and its forms)
什么是搜索? 在计算机科学中,搜索就是在一个事物的集合中找到具有特定特征的一项的过程.这些集合中的元素可能是排好序的数据库中的记录,简单数组中的数据,文件中的文本,树中的节点,几何图形中的点和边或者是 ...
- MySQL:索引工作原理
索引查找:通过索引键找到索引的叶子节点,再通过叶子节点的标记快速找到表中对应的行数据,再返回指定的列 索引找查是通过索引键定先位到一块局部区域,再开始扫描匹配的数据的. 为什么需要索引(Why is ...
- MySQL-索引工作原理及使用注意事项
1.为什么需要索引(Why is it needed)? 当数据保存在磁盘类存储介质上时,它是作为数据块存放.这些数据块是被当作一个整体来访问的,这样可以保证操作的原子性.硬盘数据块存储结构类似于链表 ...
- MySQL索引工作原理
为什么需要索引(Why is it needed)?当数据保存在磁盘类存储介质上时,它是作为数据块存放.这些数据块是被当作一个整体来访问的,这样可以保证操作的原子性.硬盘数据块存储结构类似于链表,都包 ...
- C#集合--ICollection接口和IList接口
虽然列举接口提供了一个协议,用于向前的方式遍历集合,但它们没有提供一种机制来确定集合的大小,通过索引访问集合的成员,搜索集合,或修改集合.为了实现这些功能,.NET Framework定义了IColl ...
- c++_benchMark_vector_list_deque
title: c++_benchMark_vector_list_deque date: 2015-08-01 22:32:39 作者:titer1 + ZhangYu 出处:www.drysalte ...
随机推荐
- Find the largest multiple of 3 解答
Question Given an array of non-negative integers. Find the largest multiple of 3 that can be formed ...
- 【POJ】Buy Tickets(线段树)
点更新用法很巧妙的一道题.倒序很容易的找到该人的位置,而update操作中需要不断的变化下标才能合理的插入.看了别人写的代码,学习了. #include <iostream> #inclu ...
- sae-v2ex 一个运行在SAE上的类似v2ex的轻型python论坛 - 技术讨论 - 云计算开发者社区 - Powered by Discuz!
sae-v2ex 一个运行在SAE上的类似v2ex的轻型python论坛 - 技术讨论 - 云计算开发者社区 - Powered by Discuz! sae-v2ex 一个运行在SAE上的类似v2e ...
- 【实用技术】DreamWeaver常用快捷键
文件菜单 新建文档 Ctrl+N 打开一个HTML文件 Ctrl+O 或者将文件从[文件管理器]或[站点]窗口拖动到[文档]窗口中 在框架中打开 Ctrl+Shift+O 关闭 Ctrl+W 保存 C ...
- Android面试题07
62. 说说mvc模式的原理,它在android中的运用. MVC英文即Model-View-Controller,即把一个应用的输入.处理.输出流程按照Model.View.Controller的方 ...
- ubuntu 配置jdk
shawn@e014-anle-lnx:~$ sudo su # chmod 777 jdk-6u27-linux-i586.bin # ./jdk-6u27-linux-i586.bin # mv ...
- 关于Latch
Latch是什么 Latch是SQL Server引擎保证内存中的结构的一致性的轻量同步机制.比如索引,数据页和内部结构(比如非叶级索引页).SQL Server使用Buffer Latch保护缓冲池 ...
- 谈谈UIView的几个layout方法-layoutSubviews、layoutIfNeeded、setNeedsLayout...
最近在学习swift做动画,用到constraint的动画,用到layoutIfNeeded就去研究了下UIView的这几个布局的方法. 下面是做得一个动画,下载地址:https://github.c ...
- Qt使用异或进行加密解密
在加密,解密中,异或运算应该时比较简单的一种.下面的代码,采用异或运算进行加密,解密: 点击(此处)折叠或打开 #include <QtCore/QCoreApplication&g ...
- C#枚举器接口IEnumerator的实现
原文(http://blog.csdn.net/phpxin123/article/details/7897226) 在C#中,如果一个类要使用foreach结构来实现迭代,就必须实现IEnumera ...