线性搜索python_Python线性搜索

线性搜索python

Here you will get program for linear search in python.

在这里,您将获得用于python中线性搜索的程序。

Linear search is one of the simplest searching algorithm in which targeted item in sequentially matched with each item in a list. It is worst searching algorithm with worst case time complexity O (n).

线性搜索是最简单的搜索算法之一,其中目标项目与列表中的每个项目顺序匹配。 这是最糟糕的搜索算法,具有最差的时间复杂度O(n)。

Below is its implementation.

以下是其实现。

Linear Search

Image Source

图片来源

Also Read: Python Binary Search

另请参阅: Python二进制搜索

Python线性搜索程序 (Program for Python Linear Search)

items = [5, 7, 10, 12, 15]
 
print("list of items is", items)
 
x = int(input("enter item to search:"))
 
i = flag = 0
 
while i < len(items):
	if items[i] == x:
		flag = 1
		break
 
	i = i + 1
 
if flag == 1:
	print("item found at position:", i + 1)
else:
	print("item not found")

Output

输出量

list of items is [5, 7, 10, 12, 15] enter item to search:12 item found at position: 4

项目列表为[5、7、10、12、15] 输入要搜索的 项目:在位置:4找到 12个 项目

Comment below if you have any queries regarding python linear search program.

如果您对python线性搜索程序有任何疑问,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2017/05/python-linear-search.html

线性搜索python

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值