码迷,mamicode.com
首页 > 其他好文 > 详细

递归实现二分查找

时间:2018-02-01 00:26:26      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:find   实现   ...   name   class   auth   env   body   递归   

#!/usr/bin/env python 
# -*- coding: utf-8 -*-
# @Time : 2018-01-31 20:29
# @Author : hhj
# @Site :
# @File : 递归实现二分查找.py
# def func(arg1,arg2,stop):
# if arg1==0:
# print(arg1,arg2)
# arg3=arg1+arg2
# print(arg3)
# if arg3<stop:
# func(arg2,arg3,stop) #递归
#
#
# func(0,1,100)

def binary_search(data_source,find_n):
mid=int(len(data_source)/2)
if len(data_source)>1:   # 递归结束条件
if data_source[mid]>find_n: #要找的值在左边,即:data in left
print("data in left of [%s]" %data_source[mid])
# print(data_source[:mid])
binary_search(data_source[:mid],find_n) #做切片

elif data_source[mid] < find_n: # 要找的值在左边,即:data in right
print("data in right of [%s]" % data_source[mid])
# print(data_source[mid:])
binary_search(data_source[mid:],find_n) # 做切片

else: #若
print("found data:",data_source[mid])
else:
print("connot find .....")



if __name__=="__main__":

data=list(range(0,7000000))
# print(data)
binary_search(data,35666)

递归实现二分查找

标签:find   实现   ...   name   class   auth   env   body   递归   

原文地址:https://www.cnblogs.com/hhjwqh/p/8394958.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!