Python 实现查找的几种类型 (线性查找,线性有序查找,线性查找最小值,二分查找)
来源:互联网 发布:笔记本mac地址可以改吗 编辑:程序博客网 时间:2024/04/28 07:39
#!/usr/bin/python # -*- coding: utf-8 -*-'''Created on 2015-1-15@author: beyondzhou@name: mysearch.py'''# Implementation of the linear search on an unsorted sequencedef linearSearch(theValues, target): n = len(theValues) for i in range(n): # If the target is in the ith element, return True if theValues[i] == target: return True return False # If not found, return False# Implemenation of the linear search on a sorted sequencedef sortedLinearSearch(theValues, item): n = len(theValues) for i in range(n): # If the target is found in the ith element, return True if theValues[i] == item: return True # If target is larger than the ith element, it's not in the sequence elif theValues[i] > item: return False return False# Searching for the smallest value in an unsorted sequencedef findSmallest(theValues): n = len(theValues) # Assume the first item is the smallest value smallest = theValues[0] # Determine if any other item in the sequence is smaller for i in range(1,n): if theValues[i] < smallest: smallest = theValues[i] return smallest# binary searchdef binarySearch(theValues, target): # Start with the entire sequence of elements low = 0 high = len(theValues) - 1 # Repeadedly subdivide the sequence in half until the target is found while low <= high: # Find the midpoint of the sequence mid = (high + low) // 2 # Does the midpoint contain the target? if theValues[mid] == target: return True # Or does the target precede the midpoint? elif target < theValues[mid]: high = mid - 1 # Or does it follow the midpoint? else: low = mid + 1 # If the sequence cannot be subdivided further, we're done return False
0 0
- Python 实现查找的几种类型 (线性查找,线性有序查找,线性查找最小值,二分查找)
- 线性表的查找操作(线性表的顺序查找、二分查找、分块查找)
- 数组的线性查找和二分查找
- 线性表的查找-二分查找
- 线性查找,二分查找(C语言版)
- 算法-查找-线性表的查找(顺序查找,二分查找,分块查找)
- 线性查找
- 线性查找
- 线性查找
- 线性查找
- 线性查找
- 线性查找
- 线性查找
- 查找----线性表的查找
- C语言实现的线性查找和二分查找
- 线性查找与二分查找(java实现)
- 查找算法(2):线性结构的二分查找
- 线性查找---JAVA实现
- Activity生命周期
- 办公自动化,内容部分无法显示的处理方法
- 开发板挂载Ubuntu文件夹
- 700+Android源码例子上传供大家下载参考
- 社説 20150115 15年度予算案 経済再生を着実に実現したい
- Python 实现查找的几种类型 (线性查找,线性有序查找,线性查找最小值,二分查找)
- WPF搜索关键字高亮显示
- PHP使用HMAC-SHA1+base64签名方法
- iOS开发常用宏定义
- 黑马程序员——OC语言——block、protocol、代理模式
- 【笔记】原来继承的方法有那么多
- TP框架开发的站点使用phpdoctor生成文档注意事项
- 关于栈溢出和手工开栈
- 1.ul中li整个横向居中显示问题