有序线性搜索(Sorted/Ordered Linear Search)
如果数组元素已经排过序(升序),那我们搜索某个元素就不必遍历整个数组了。在下面给出的算法代码中,到任何一点,假设当前的arr[i]值大于搜索的值data,就可以停止搜索了。
#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 orderedLinearSearch(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;
}
// here is an additional check
else if(arr[i] > data)
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(orderedLinearSearch(arr,10,to_search))
printf("FOUND");
else
printf("NOT FOUND"); return 0;
}
算法的时间复杂度为O(n)。这是因为在最差的情况下我们仍然要搜索整个数组。虽然增长率和无序线性搜索一样,但在平均情况下减少了复杂度。
空间复杂度为O(1)。
注:我们还可以增加索引增加的速率来提高算法速度。这样会减少算法中比较的次数。但这样会有几率跳过所要搜索的数据。
有序线性搜索(Sorted/Ordered Linear Search)的更多相关文章
- 无序线性搜索(Unordered Linear Search)
假定有一个元素顺序情况不明的数组.这种情况如果我们要搜索一个元素就要遍历整个数组,才能知道这个元素是否在数组中. 这种方法要检查整个数组,核对每个元素.下面是算法实现: #include<std ...
- 33. Search in Rotated Sorted Array & 81. Search in Rotated Sorted Array II
33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...
- LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>
LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...
- python 操作redis之——有序集合(sorted set) (七)
#coding:utf8 import redis r =redis.Redis(host=") 1.Zadd 命令用于将一个或多个成员元素及其分数值加入到有序集当中.如果某个成员已经是有序 ...
- Redis 有序集合(sorted set),发布订阅,事务,脚本,连接,服务器(三)
Redis 有序集合(sorted set) Redis 有序集合和集合一样也是string类型元素的集合,且不允许重复的成员. 不同的是每个元素都会关联一个double类型的分数.redis正是通过 ...
- (PHP)redis Zset(有序集合 sorted set)操作
/** * * Zset操作 * sorted set操作 * 有序集合 * sorted set 它在set的基础上增加了一个顺序属性,这一属性在修改添加元素的时候可以指定,每次指定后,zset会自 ...
- redis(十四):Redis 有序集合(sorted set)
Redis 有序集合(sorted set) Redis 有序集合和集合一样也是string类型元素的集合,且不允许重复的成员. 不同的是每个元素都会关联一个double类型的分数.redis正是通过 ...
- [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. ...
- leetCode题解之寻找一个数在有序数组中的范围Search for a Range
1.问题描述 Given an array of integers sorted in ascending order, find the starting and ending position o ...
随机推荐
- Paint House 解答
Question There are a row of n houses, each house can be painted with one of the three colors: red, b ...
- High bridge, low bridge(离散化, 前缀和)
High bridge, low bridge Q:There are one high bridge and one low bridge across the river. The river h ...
- [think in java]知识点学习
java中 全部数值都有正负号,不存在无符号整数. java中的基本类型存储在堆栈中. 其它对象存储在堆中. java确保数组会被初始化,并且不能在它的范围之外被訪问. 下面代码在c和c++中是合法的 ...
- 訪问远程WAMP 下phpmyadmin
WAMP环境是一个非常优秀的webservice集成环境,它集成的phpmyadmin也是一款非常优秀的数据库訪问软件.wamp默认安装下,phpmyadmin工具仅仅能本地用,在站点开发中,数据库都 ...
- [CSS3] CSS Display Property: Block, Inline-Block, and Inline
Understanding the most common CSS display types of block, inline-block, and inline will allow you to ...
- 参数化SQL小认识
在做机房收费系统项目,编写数据连接并访问数据库时,见别人都用了带“@”字符的SQL语句,就很好奇为什么都用这个语句呢?直接拼写SQL语句不是更加方便吗?带着这个问题上网查资料,才知道原来他们用的是参数 ...
- Swift学习之函数和简单地控件的创建
今天还是重复昨天做的事情--敲代码,但唯一的不同就是所学的知识不同了,我们又进一步往深得层次学习了,感觉越来越有意思了,虽然临近结束了看着大家积极性越来越低了,但是我知道我不能这样,我要比别人付出的 ...
- Linux下Ant的安装
OS:CentOS6.3 ant版本:apache-ant-1.9.2-bin 第1步:下载ant apache-ant-1.9.2-bin.tar.gz 第2步:解压 tar -zxvf apach ...
- AE分级渲染
分级渲染classbreakrenderer位于carto类库中,进行分级渲染时,首先要将相应图层按照某一Field分级.可使用esrisystem类库中的iclassifyGEN类的classify ...
- Asp.net 网站出现Service Unavailable 问题剖析
网站出现这样的情况,而且刷新一下又重新正常. 个人分析认为造成原因如下: 1.应用程序池配置存在问题. 2.程序中存在没有关闭的连接数据库对象,或者含有死循环. 3.程序中产生的内存数据量太多,导致网 ...