线性搜索是在数组中搜索元素的最简单方法。它是一种顺序搜索算法,从一端开始,并检查数组的每个元素,直到找到所需的元素。
递归是指一个函数调用自身,当使用递归函数时,我们需要使用任何循环来生成迭代。下面的语法演示了简单递归函数的工作原理。
def rerecursiveFun(): Statements ... rerecursiveFun() ... rerecursiveFun
递归地线性搜索元素
从数组中递归地线性搜索一个元素,只能通过使用函数来实现。在Python中,要定义一个函数,我们需要使用def关键字。
在本文中,我们将学习如何在Python中递归线性搜索数组中的元素。在这里,我们将使用Python列表代替数组,因为Python没有特定的数据类型来表示数组。
Example
我们将通过递减数组的大小来递归调用函数 recLinearSearch()。如果数组的大小变为负数,意味着该元素不在数组中,我们返回 -1。如果找到匹配项,则返回元素所在的索引位置。
# Recursively Linearly Search an Element in an Array def recLinearSearch( arr, l, r, x): if r < l: return -1 if arr[l] == x: return l if arr[r] == x: return r return recLinearSearch(arr, l+1, r-1, x) lst = [1, 6, 4, 9, 2, 8] element = 2 res = recLinearSearch(lst, 0, len(lst)-1, element) if res != -1: print('{} was found at index {}.'.format(element, res)) else: print('{} was not found.'.format(element))
输出
2 was found at index 4.
Example
让我们来看一个在数组中搜索元素的另一个例子。
# Recursively Linearly Search an Element in an Array def recLinearSearch(arr, curr_index, key): if curr_index == -1: return -1 if arr[curr_index] == key: return curr_index return recLinearSearch(arr, curr_index-1, key) arr = [1, 3, 6, 9, 12, 15] element = 6 res = recLinearSearch(arr, len(arr)-1, element) if res != -1: print('{} was found at index {}.'.format(element, res)) else: print('{} was not found.'.format(element))
输出
6 was found at index 2.
Example
以搜索数组中的元素100为另一个例子。
# Recursively Linearly Search an Element in an Array def recLinearSearch(arr, curr_index, key): if curr_index == -1: return -1 if arr[curr_index] == key: return curr_index return recLinearSearch(arr, curr_index-1, key) arr = [1, 3, 6, 9, 12, 15] element = 100 res = recLinearSearch(arr, len(arr)-1, element) if res != -1: print('{} was found at index {}.'.format(element, res)) else: print('{} was not found.'.format(element))
输出
100 was not found.
在上面的示例中,给定的数组中找不到元素100。
这些是使用Python编程递归线性搜索数组中元素的示例。
以上是Python程序递归线性搜索数组中的元素的详细内容。更多信息请关注PHP中文网其他相关文章!

Python列表切片的基本语法是list[start:stop:step]。1.start是包含的第一个元素索引,2.stop是排除的第一个元素索引,3.step决定元素之间的步长。切片不仅用于提取数据,还可以修改和反转列表。

ListSoutPerformarRaysin:1)DynamicsizicsizingandFrequentInsertions/删除,2)储存的二聚体和3)MemoryFeliceFiceForceforseforsparsedata,butmayhaveslightperformancecostsinclentoperations。

toConvertapythonarraytoalist,usEthelist()constructororageneratorexpression.1)intimpthearraymoduleandcreateanArray.2)USELIST(ARR)或[XFORXINARR] to ConconverTittoalist,请考虑performorefformanceandmemoryfformanceandmemoryfformienceforlargedAtasetset。

choosearraysoverlistsinpythonforbetterperformanceandmemoryfliceSpecificScenarios.1)largenumericaldatasets:arraysreducememoryusage.2)绩效 - 临界杂货:arraysoffersoffersOffersOffersOffersPoostSfoostSforsssfortasssfortaskslikeappensearch orearch.3)testessenforcety:arraysenforce:arraysenforc

在Python中,可以使用for循环、enumerate和列表推导式遍历列表;在Java中,可以使用传统for循环和增强for循环遍历数组。1.Python列表遍历方法包括:for循环、enumerate和列表推导式。2.Java数组遍历方法包括:传统for循环和增强for循环。

本文讨论了Python版本3.10中介绍的新“匹配”语句,该语句与其他语言相同。它增强了代码的可读性,并为传统的if-elif-el提供了性能优势

Python中的功能注释将元数据添加到函数中,以进行类型检查,文档和IDE支持。它们增强了代码的可读性,维护,并且在API开发,数据科学和图书馆创建中至关重要。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

WebStorm Mac版
好用的JavaScript开发工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器