#!/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)
递归实现二分查找
Posted hhjwqh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了递归实现二分查找相关的知识,希望对你有一定的参考价值。
以上是关于递归实现二分查找的主要内容,如果未能解决你的问题,请参考以下文章